Skip to content

Instantly share code, notes, and snippets.

@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
{-# 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: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>
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 / 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
@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 / 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 / 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
data Consumer i o = C { consume :: Maybe i -> Result i o }
data Result i o =
Done o
| Cont (Consumer i o)
@dtchepak
dtchepak / gist:5889648
Created June 29, 2013 03:53
Example of NSubstitute type mismatch exception for nested returns.
Can not return value of type IFooProxy for IFoo.set_SomeString (expected type Void).
Make sure you called Returns() after calling your substitute (for example: mySub.SomeMethod().Returns(value)),
and that you are not configuring other substitutes within Returns() (for example, avoid this: mySub.SomeMethod().Returns(ConfigOtherSub())).
If you substituted for a class rather than an interface, check that the call to your substitute was on a virtual/abstract member.
Return values cannot be configured for non-virtual/non-abstract members.
Correct use:
mySub.SomeMethod().Returns(returnValue);