Skip to content

Instantly share code, notes, and snippets.

data Consumer i o = C { consume :: Maybe i -> Result i o }
data Result i o =
Done o
| Cont (Consumer i o)
@dtchepak
dtchepak / freq.hs
Last active December 19, 2015 17:09
Count number of occurrences of each item in a list
import Control.Arrow ((&&&))
import Control.Monad.State
import Data.List as L
import Data.Map as M
import Data.Ord
import Data.Traversable
type Freq a = [(a,Int)]
freq :: Ord a => [a] -> Freq a
@dtchepak
dtchepak / gist:6314088
Created August 22, 2013 23:46
~/.ghci
:set prompt "ghci> "
-- Configure :pf to call pointfree command line tool
:def pf \str -> return $ ":! pointfree \"" ++ str ++ "\""
-- Hoogle
:def hoogle \str -> return $ ":! hoogle --count=15 \"" ++ str ++ "\""
@dtchepak
dtchepak / NSubMisc.cs
Created September 18, 2013 05:25
Example from NSub list
using System.Collections.Generic;
using System.Linq;
using NSubstitute;
using NUnit.Framework;
using Shouldly;
namespace Workshop
{
public class NSubMisc
{
@dtchepak
dtchepak / BorrowABook.fs
Last active December 26, 2015 14:49
Trying to port Liam's simplified controller example to F#: http://withouttheloop.com/articles/2013-10-20-are-we-doing-mvc-wrong/ Untested.
module BorrowABook =
type BookLoanProblem =
NotInCatalogue
| OutOnLoan
| InsufficientPermission
| Failed of string
type Book = Book
type BookModel = BookModel
public abstract class Thingoe {
public Thingoe(string id, int quantity) { ... }
public string Id { get; private set; }
public int Quantity { get; private set; }
}
public class WidgetThingoe : Thingoe {
public WidgetThingoe(string id, int quantity) : base(id,quantity) {}
}
public class OtherThingoe : Thingoe {
@dtchepak
dtchepak / gist:7332004
Last active December 27, 2015 13:19
Changes to fsproj created by VS2013 in order to build F# project from the command line. (Solved, see comment)
<PropertyGroup>
<MinimumVisualStudioVersion Condition="'$(MinimumVisualStudioVersion)' == ''">11</MinimumVisualStudioVersion>
</PropertyGroup>
<Choose>
- <When Condition="'$(VisualStudioVersion)' == '11.0'">
- <PropertyGroup Condition="Exists('$(MSBuildExtensionsPath32)\..\Microsoft SDKs\F#\3.0\Framework\v4.0\Microsoft.FSharp.Targets')">
- <FSharpTargetsPath>$(MSBuildExtensionsPath32)\..\Microsoft SDKs\F#\3.0\Framework\v4.0\Microsoft.FSharp.Targets</FSharpTargetsPath>
+ <When Condition=" Exists('$(MSBuildExtensionsPath32)\..\Microsoft SDKs\F#\3.1\Framework\v4.0\Microsoft.FSharp.Targets')">
+ <PropertyGroup>
+ <FSharpTargetsPath>$(MSBuildExtensionsPath32)\..\Microsoft SDKs\F#\3.1\Framework\v4.0\Microsoft.FSharp.Targets</FSharpTargetsPath>
{-# LANGUAGE DeriveFunctor #-}
import Control.Monad.Free
--data Free f a = Pure a | Free (f (Free f a))
--instance Functor f => Monad (Free f) where
-- return = Pure
-- (Pure a) >>= f = f a
-- (Free fr) >>= f = Free (fmap (>>= f) fr)
--
--liftF :: Functor f => f a -> Free f a
@dtchepak
dtchepak / gist:7441120
Created November 13, 2013 00:07
Which do you prefer out of `someFunction` and `someFunction2`? Which do you prefer overall? Which is most idiomatic F#?
// names and types combined
let someFunction (a:string) (b:int) : string =
a + string b
// type first, then name args
let someFunction2 : string -> int -> string = fun a b ->
a + string b
void Main()
{
var x = new Thing();
Action<string> onFoo = s => Console.WriteLine (s);
x.OnFoo += onFoo.Invoke;
x.RaiseEvent("a");
x.RaiseEvent("b");
x.OnFoo -= onFoo.Invoke;
x.RaiseEvent("c");
}