Skip to content

Instantly share code, notes, and snippets.

@cloudRoutine
Created December 24, 2014 08:10
Show Gist options
  • Select an option

  • Save cloudRoutine/924a990e798182043f00 to your computer and use it in GitHub Desktop.

Select an option

Save cloudRoutine/924a990e798182043f00 to your computer and use it in GitHub Desktop.
Morphisms
module Morphisms =
/// Hylomorphism that composes an unfold into a fold
/// folding an unfold
// ( fold o unfold )(x) = fold (unfold(x))
let hylomorphism
(ufstepfn:#seq<'b>->'b->#seq<'b>)(ufacc:#seq<'b>)(pred:'a->bool)(mapElm:'a->'b) (inc:'a->'a)
(fstepfn:'c->'b->'c) (foldAcc :'c) : 'a -> 'c =
(Folds.unfold ufstepfn ufacc pred mapElm inc ) >> (Folds.foldl fstepfn foldAcc )
/// Metamorphism that compose aFolds.foldl into anFolds.unfold
// (unfold oFolds.foldll)(x) =Folds.unfold(foldl(x))
let metamorphism
(stepfn:'b->'a->'b) (acc :'b)
(pred :'b->bool) (mapElm:'b->'c) (inc:'b->'b) : (#seq<'a>->'c list)=
(Folds.foldl stepfn acc ) >> (Folds.unfoldls pred mapElm inc)
///Folds.foldling a hylomorphism
let hylomorphismfold
(pred :'a->bool) (mapElm:'a->'b) (mapSeed:'a->'a)
(stepfn :'c->'b->'c) (acc :'c)
(stepfn2:'d->'c->'d) (acc2 :'d) : ('a -> 'd) =
((Folds.unfoldls pred mapElm mapSeed ) >> (Folds.foldl stepfn acc ))
>> (Folds.foldl stepfn2 acc2 )
let metafold
(stepfn:'b->'a->'b) (acc :'b)
(pred :'b->bool) (mapElm:'b->'c) (inc:'b->'b)
(stepfn2:'d->'c->'d) (acc2 :'d) : (#seq<'a>->'d) =
(Folds.foldl stepfn acc ) >> (Folds.unfoldls pred mapElm inc)
>> (Folds.foldl stepfn2 acc2 )
/// Metamorphing a hylomorphism
let metahylo
(stepfn1:'c->'b->'c) (acc1 :'c)
(pred1 :'a->bool) (mapElm1:'a->'b) (inc1:'a->'a)
(pred2 :'d->bool) (mapElm2:'d->'e) (inc2:'d->'d)
(stepfn2:'d->'c->'d) (acc2 :'d) : ('a -> 'e list) =
((Folds.unfoldls pred1 mapElm1 inc1 ) >> (Folds.foldl stepfn1 acc1 ))
>> ((Folds.foldl stepfn2 acc2 ) >> (Folds.unfoldls pred2 mapElm2 inc2 ))
let hylometa
(stepfn1) (acc1 )
(pred1 ) (mapElm1) (inc1)
(pred2 ) (mapElm2) (inc2)
(stepfn2) (acc2 ) : #seq<'a>->'e =
((Folds.foldl stepfn2 acc2 ) >> (Folds.unfold pred2 mapElm2 inc2 ))
>> ((Folds.unfoldls pred1 mapElm1 inc1 ) >> (Folds.foldl stepfn1 acc1 ))
let hylols
(pred :'a->bool) (mapElm:'a->'b) (inc:'a->'a)
(stepfn:'c->'b->'c) (acc :'c) : ('a -> 'c) =
(Folds.unfoldls pred mapElm inc ) >> (Folds.foldl stepfn acc )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment