Skip to content

Instantly share code, notes, and snippets.

import cats.effect._
import cats.effect.unsafe.implicits.global
import fs2._
import java.time.Instant
import scala.concurrent.duration._
object Mine extends App {
val s =
module Main where
import Lib
{-# LANGUAGE OverloadedStrings #-}
{-# OPTIONS -Wall -fno-warn-type-defaults #-}
import Graphics.Gloss
import Data.Monoid
window :: Display
name: sierpinski
version: 0.1.0.0
github: "githubuser/sierpinski"
license: BSD3
author: "Author name here"
maintainer: "example@example.com"
copyright: "2021 Author name here"
extra-source-files:
- README.md
# This file was automatically generated by 'stack init'
#
# Some commonly used options have been documented as comments in this file.
# For advanced use and comprehensive documentation of the format, please see:
# https://docs.haskellstack.org/en/stable/yaml_configuration/
# Resolver to choose a 'specific' stackage snapshot or a compiler version.
# A snapshot resolver dictates the compiler version and the set of packages
# to be used for project dependencies. For example:
#
flatten is indeed an interesting topic - one cool thing is that flatMap is just map followed by flatten, i.e. (ma flatMap f) == (ma map f flatten) [1].
Another is that flatten is flatMap(identity) [2][3].
Another is that a monad can be defined as a functor with a unit function (i.e. a type constructor like List or Option) and a flatten function (aka join in Haskell) [4]
[1] https://github.com/philipschwarz/scala-fp-combinators-code-kata/blob/master/src/main/scala/FPCombinatorsCodeKata.scala#L18 - https://www.slideshare.net/pjschwarz/scala-functional-programming-combinators-code-kata
[2] https://github.com/philipschwarz/scala-fp-combinators-code-kata/blob/master/src/main/scala/FPCombinatorsCodeKata.scala#L20
[3] https://www.slideshare.net/pjschwarz/symmetry-in-the-interrelation-of-flatmapfoldmaptraverse-and-flattenfoldsequence
[4] https://www.slideshare.net/pjschwarz/monad-as-functor-with-pair-of-natural-transformations#8
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 .........................................." ;'⑦'
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 []
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
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