Skip to content

Instantly share code, notes, and snippets.

@jbrestan
Created January 7, 2016 13:12
Show Gist options
  • Save jbrestan/4295e11dc911d17b172d to your computer and use it in GitHub Desktop.
Save jbrestan/4295e11dc911d17b172d to your computer and use it in GitHub Desktop.
General reducees F# implementation, based on this http://blog.plataformatec.com.br/2015/05/introducing-reducees/
module Reducees =
type Instruction = Cont | Done | Halt
let rec reduce source instruction func =
match source, instruction with
| head::tail, (Cont, acc) -> reduce tail (func head acc) func
| [], (Cont, acc) -> Done, acc
| _ -> instruction
open Reducees
let map coll func =
let result = reduce coll (Cont, []) (fun x acc -> Cont, ((func x)::acc))
match result with Done, res -> List.rev res | _ -> []
printf "%A" <| map [1;2;3;4] ((*)2)
let take coll n =
let nextInstruction n = if n = 1 then Halt else Cont
let result = reduce coll (Cont, ([], n)) (fun x (acc, count) -> (nextInstruction count), (x::acc, n-1))
match result with _, (res, _) -> List.rev res
printf "%A" <| take [1;2;3;4;5] 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment