mostlyadequate

package
v1.0.22 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 1, 2023 License: Apache-2.0 Imports: 0 Imported by: 0

README

Mostly Adequate: fp-go Companion Guide

This resource is meant to serve as a go "companion" resource to Professor Frisby's Mostly Adequate Guide.

It is a port of the mostly-adequate-fp-ts book.

Documentation

Overview

Package mostlyadequate is meant to serve as a go "companion" resource to Professor Frisby's Mostly Adequate Guide.

Example (Dasherize)
fmt.Println(Dasherize("The world is a vampire"))
Output:

the-world-is-a-vampire
Example (Flock)
package main

import "fmt"

type Flock struct {
	Seagulls int
}

func MakeFlock(n int) Flock {
	return Flock{Seagulls: n}
}

func (f *Flock) Conjoin(other *Flock) *Flock {
	f.Seagulls += other.Seagulls
	return f
}

func (f *Flock) Breed(other *Flock) *Flock {
	f.Seagulls = f.Seagulls * other.Seagulls
	return f
}

func main() {

	flockA := MakeFlock(4)
	flockB := MakeFlock(2)
	flockC := MakeFlock(0)

	fmt.Println(flockA.Conjoin(&flockC).Breed(&flockB).Conjoin(flockA.Breed(&flockB)).Seagulls)

}
Output:

32
Example (GetAge)
now, err := time.Parse(time.DateOnly, "2023-09-01")
if err != nil {
	panic(err)
}

fmt.Println(GetAge(now)(MakeUser("2005-12-12")))
fmt.Println(GetAge(now)(MakeUser("July 4, 2001")))

fortune := F.Flow3(
	N.Add(365.0),
	S.Format[float64]("%0.0f"),
	Concat("If you survive, you will be "),
)

zoltar := F.Flow3(
	GetAge(now),
	E.Map[error](fortune),
	E.GetOrElse(errors.ToString),
)

fmt.Println(zoltar(MakeUser("2005-12-12")))
Output:

Right[<nil>, float64](6472)
Left[*time.ParseError, float64](parsing time "July 4, 2001" as "2006-01-02": cannot parse "July 4, 2001" as "2006")
If you survive, you will be 6837
Example (Pipe)
output := F.Pipe2(
	"send in the clowns",
	ToUpper,
	Exclaim,
)

fmt.Println(output)
Output:

SEND IN THE CLOWNS!
Example (Shout)
fmt.Println(Shout("send in the clowns"))
Output:

SEND IN THE CLOWNS!
Example (Street)
package main

import (
	"fmt"

	A "github.com/IBM/fp-go/array"
	F "github.com/IBM/fp-go/function"
	O "github.com/IBM/fp-go/option"
)

type (
	Street struct {
		Name   string
		Number int
	}

	Address struct {
		Street   Street
		Postcode string
	}

	AddressBook struct {
		Addresses []Address
	}
)

func getAddresses(ab AddressBook) []Address {
	return ab.Addresses
}

func getStreet(s Address) Street {
	return s.Street
}

var FirstAddressStreet = F.Flow3(
	getAddresses,
	A.Head[Address],
	O.Map(getStreet),
)

func main() {
	s := FirstAddressStreet(AddressBook{
		Addresses: A.From(Address{Street: Street{Name: "Mulburry", Number: 8402}, Postcode: "WC2N"}),
	})
	fmt.Println(s)

}
Output:

Some[mostlyadequate.Street]({Mulburry 8402})
Example (Widthdraw)
fmt.Println(getTwenty(MakeAccount(200)))
fmt.Println(getTwenty(MakeAccount(10)))
Output:

Your balance is $180.00
You're broke!

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL