Skip to content

Instantly share code, notes, and snippets.

import java.util.{Date, Calendar}
import util.{Try, Success, Failure}
type Amount = BigDecimal
def today = Calendar.getInstance.getTime
case class Balance(amount: Amount = 0)
sealed trait Account {
import scalaz.std.vector.vectorMonoid
import scalaz.syntax.foldable.ToFoldableOps
import scalaz.syntax.semigroup._
import scalaz.std.list.listInstance
// Vector append and prepend functions
assert( Vector(1,2) :+ 3 == Vector(1,2,3) )
assert( 1 +: Vector(2,3) == Vector(1,2,3) )
fizzbuzz :: [String]
fizzbuzz =
let fizzes = cycle ["", "", "fizz"]
buzzes = cycle ["", "", "", "", "buzz"]
pattern = zipWith (++) fizzes buzzes
in zipWith combine pattern [1..] where
combine word number =
if null word then show number else word
-- Solution to https://adventofcode.com/2019/day/1 by @philip_schwarz
-- Based on following solution by @runarorama:
-- https://gist.github.com/runarorama/21e6876bd62812b9d61a312c75ec87a3
use .base
use .base.test.internals.v1.Test forAll
use .base.Test
-- Part 1
type FruitSalad = {
Apple: AppleVariety
Banana: BananaVariety
Cherries: CherryVariety
}
use .base
use List Optional
List.head : [a] -> Optional a
List.head a = List.at 0 a
ability State s where
put : s -> {State s} ()
get : {State s} s
use .base
use List Optional
List.head : [a] -> Optional a
List.head a = List.at 0 a
ability State s where
put : s -> {State s} ()
get : {State s} s
use .base
use List Optional Some None drop map
List.head : [a] -> Optional a
List.head a = List.at 0 a
use List head
ability State s where
put : s -> {State s} ()
get : {State s} s
base.List.map : (a ->{𝕖} b) -> [a] ->{𝕖} [b]
base.List.map f a =
go i as acc =
match List.at i as with
None -> acc
Some a ->
use Nat +
go (i + 1) as (acc :+ f a)
go 0 a []
object Main extends App {
" SCALA FP COMBINATORS CODE KATA - start with expression 'ma flatMap f' and keep refactoring it by "
" applying each of the following rewrite rules in turn, until you get back to 'ma flatMap f' "
'①'; " flatmap can be defined in terms of map and flatten ......................................" ;'①'
'②'; " map can be defined in terms of flatMap and pure ........................................." ;'②'
'③'; " flatten can be defined in terms of flatMap and identity ................................." ;'③'
'④'; " chained flatMaps are equivalent to nested flatMaps (flatMap associativity law) .........." ;'④'
'⑤'; " Kleisli composition can be defined in terms of flatMap (apply this the other way around) " ;'⑤'
'⑥'; " the identity function can be defined in terms of flatten and pure ......................." ;'⑥'
'⑦'; " pure followed by flatten cancel each other out .........................................." ;'⑦'