Snippets in category Design Patterns
Projecting lists
Three functions showing how to implement projection for functional lists. First version uses naive recursion and the second one is tail-recursive using the accumulator parameter. The third version extends this with continuation passing.
43 people like this
Posted: 1 years ago by Tomas PetricekMemoization and Tail Recursive Function
Hi, I expressed Memoization and Memoization Tail Recursive on the functions. I hope something useful.
12 people like this
Posted: 1 years ago by zeclTree searching using Tail recursion with continuation
Sample code which demonstrate tree searching using : Tail recursion with continuation
9 people like this
Posted: 1 years ago by Ankur DhamaAbstracting over 'M'
A higher kind of request to Don Syme... please please please,,,, we desperately need higher kinds!
13 people like this
Posted: 1 years ago by Nick PalladinosMonadic Memoization
Modular memoization within a pure functional setting that is implemented as a convenient computation builder.
56 people like this
Posted: 1 years ago by Nick PalladinosFunctor => Applicative => Monad
Yet another attempt of mine to "haskellify" my F# coding.
8 people like this
Posted: 1 years ago by Nick PalladinosUndoable Commands
A simple implementation of an Undoable Command, with a Document to hold a stack of actions. 2 examples of UndoableCommand are given - 1 allows property changes to be remembered, and another which allows the user to execute an action with a corresponding undo. Further examples could include CompositeUndoableCommands where the command is itself a list of commands.
0 people like this
Posted: 1 years ago by Neil DansonmemoizeBy
Sometimes you might wish to memoize a function whose input doesn't have the equality and comparison constraints, or maybe the comparison of your given type is just too slow for what you need. To fix this, you simply provide a function which converts the input into something more fitting as an extra parameter.
7 people like this
Posted: 10 months ago by Rick MinerichContinuation Monad with Call/CC
This is an implementation of the Continuation monad using a type, taking an exception handler, and allowing for Call/CC. This specific implementation is mostly Matt Podwysocki's. I have a similar implementation using a purely functional, exception-handler-less version in FSharp.Monad. Until now, I haven't been able to resolve the callCC operator.
3 people like this
Posted: 9 months ago by Ryan RileyChain of responsibility
The following sample wants to make sure the person’s age is between 18 and 65, weight is no more than 200 and tall enough (>120).
64 people like this
Posted: 9 months ago by Tao LiuDecorate pattern
implement the decorate pattern in F#. The decorate pattern is to add new featuures to an object at runtime.
35 people like this
Posted: 9 months ago by Tao LiuObserver pattern
Observer pattern in F#
33 people like this
Posted: 9 months ago by Tao LiuStrategy pattern
Strategy pattern in F#
59 people like this
Posted: 9 months ago by Tao LiuState Pattern
the interest rate is decided by the internal state: account balance
30 people like this
Posted: 9 months ago by Tao LiuAdapter pattern
Invoke the methods from incompatible types
72 people like this
Posted: 9 months ago by Tao Liu
Command pattern for Redo-Undo
Command pattern for redo-undo scenario.
43 people like this
Posted: 9 months ago by Tao LiuComposite pattern and Visitor Pattern
use composite pattern to construct a tree and visitor pattern to bring back the traverse result.
31 people like this
Posted: 9 months ago by Tao LiuSingleton Pattern
singleton pattern in F#
34 people like this
Posted: 9 months ago by Tao LiuFactory Pattern
Factory pattern in F#
57 people like this
Posted: 9 months ago by Tao LiuProxy Pattern
Proxy pattern is a class functioning as an interface to something else.
37 people like this
Posted: 9 months ago by Tao LiuChain of responsibility II
Unlike the previous chain of responsibility, this version use the pipeline to chain responsibilities.
79 people like this
Posted: 8 months ago by Tao LiuCommand pattern for Redo-Undo II
This command redo-undo implement group the command under Do/Undo category.
58 people like this
Posted: 8 months ago by Tao LiuCall/CC for Async
An implementation of call-with-current-continuation for Async.
2 people like this
Posted: 8 months ago by Ryan RileyMemoization for dynamic programming
The snippet shows how to implement reusable memoization function and how to use it to implement efficient Fibonacci number generator using dynamic programming.
1 people like this
Posted: 6 months ago by Tomas PetricekTemplate pattern
Template pattern is a behavior-based pattern. Realized the importance of high order function, this is a way to use high order function. Still keep the type structure to organize my code.
34 people like this
Posted: 6 months ago by Tao LiuPoor Man's DI
Minimal Dependency Injection (DI) Container in under 60 lines of code implementing the 3 Rs: Register, Resolve, Release. Note: missing thread safety, explicit cycle dependency checking and Fluent interface.
6 people like this
Posted: 5 months ago by Phillip TrelfordDSL for financial contracts
Simple domain-specific language (DSL) for describing financial contracts in F#. A contract is represented using a discriminated union. Evaluating a contract gives the orders that may happen at a given date.
6 people like this
Posted: 5 months ago by Tomas PetricekPolymorphic Maybe monad with default value.
Polymorphic (via generics) Maybe monad/computational expression with default value + Zipper
4 people like this
Posted: 2 months ago by DaniilExpression parsing with monads
Compositional expression parsing with monads. https://bitbucket.org/ZachBray/parsad
4 people like this
Posted: 1 months ago by Zach BrayJSON parsing with monads
JSON parsing with monads. See also "Expression parsing with monads" (http://fssnip.net/bi). Author URL: http://www.zbray.com
5 people like this
Posted: 1 months ago by Zach Bray