Skip to content

Instantly share code, notes, and snippets.

@giuliohome
giuliohome / simple_wcf.fs
Last active November 6, 2016 13:53 — forked from dgfitch/simple_wcf.fs
A sample WCF host and client in F#, extremely simple but a starting point
open System
open System.ServiceModel
open System.ServiceModel.Description
[<ServiceContract(ConfigurationName = "PublishService", Namespace = "http://xyz.gov/PublishService")>]
[<ServiceBehavior(InstanceContextMode=InstanceContextMode.PerSession)>]
type IPublishService =
[<OperationContract(Name="TestMethod")>]
abstract member TestMethod : name:string -> string
using System;
using System.Collections.Generic;
using System.Linq;
using TransducersNet;
namespace TransducerSample2CSharp
{
class Program
{
private static IEnumerable<string> asStream(string[] iterable)
// https://dotnetfiddle.net/sjSjeS
open System
open System.Data
open System.Xml
open Microsoft.FSharp.Linq
type RowReader(reader:DataRow) =
member private x.Reader = reader
static member (?) (x:RowReader, name:string) : 'R =
@giuliohome
giuliohome / io.fsx
Created June 6, 2017 15:15 — forked from battermann/io.fsx
IO Monad in F#
[<AutoOpen>]
module IO =
type IO<'a> =
private
| Return of (unit -> 'a)
| Suspend of (unit -> IO<'a>)
let rec run x =
match x with
| Return v -> v()
type TrimNonEmptyString internal (value : string) =
member __.Value = value
override __.ToString() = value
override __.Equals(yobj) =
match yobj with
| :? TrimNonEmptyString as y -> (__.Value = y.Value)
| _ -> false
override __.GetHashCode() = hash value
static member TryParse (value : string) =
if System.String.IsNullOrWhiteSpace value then
@giuliohome
giuliohome / free.fsx
Created August 1, 2017 17:29 — forked from battermann/free.fsx
Free Monad like pattern in F#
#load @"paket-files/fsprojects/Chessie/src/Chessie/ErrorHandling.fs"
type Continuation<'output, 'next> = 'output -> 'next
module TerminalDsl =
open Chessie.ErrorHandling
type Terminal<'next> =
| WriteLine of string * Continuation<unit, 'next>
| ReadLine of unit * Continuation<string, 'next>
@giuliohome
giuliohome / Primes.hs
Created December 29, 2017 14:12
Applying f-algebras to primes
{-# LANGUAGE DeriveFunctor #-}
-- https://bartoszmilewski.com/2017/02/28/f-algebras/
newtype Fix f = Fix (f (Fix f))
unFix :: Fix f -> f (Fix f)
unFix (Fix x) = x
cata :: Functor f => (f a -> a) -> Fix f -> a
cata alg = alg . fmap (cata alg) . unFix
ana :: Functor f => (a -> f a) -> a -> Fix f
@giuliohome
giuliohome / dsl.hs
Created December 30, 2017 00:48
A complete example of DSL in Haskell
{-# LANGUAGE DeriveFunctor #-}
-- {-# LANGUAGE GeneralizedNewtypeDeriving #-}
-- {-# LANGUAGE XRankNTypes #-}
-- run this online https://repl.it/repls/GraciousCooperativeNijssenissdwarfchihlid
import Control.Applicative -- <$>
import Control.Monad
data Free f r = Free (f (Free f r)) | Pure r
{-# LANGUAGE DeriveFunctor #-}
-- Is there a trick to getting this done easier?
-- yep, look at TryAgainFromScratch.hs
module Bridge where
import Data.List
import Data.Ord
import qualified Data.Map as Map
@giuliohome
giuliohome / Hylo.fs
Created February 19, 2018 07:08 — forked from CarstenKoenig/Hylo.fs
Factorial using a Hylomorphism in F#
type List<'i,'r> = Nil | Cons of 'i*'r
type FixList<'i> = FixList of List<'i,FixList<'i>>
let rec fmap (f : 'a -> 'b) (l : List<'i,'a>) : List<'i,'b> =
match l with
| Nil -> Nil
| Cons (x, tail) -> Cons (x, f tail)
// you can express hylo directly without using ana and cata (by either following the