Skip to content

Instantly share code, notes, and snippets.

View akimboyko's full-sized avatar
🙃

Akim Boyko akimboyko

🙃
View GitHub Profile
@akimboyko
akimboyko / Sample01.cs
Created December 11, 2014 09:44
F# decompiled pattern matching
// Decompiled with JetBrains decompiler
// Type: FSharpParts.FSharpParts
// Assembly: FSharpParts, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: 54894392-FF35-398D-A745-038392438954
// Assembly location: D:\temp\SharpExperiments\FSharpParts\bin\Debug\FSharpParts.dll
using CSharpParts;
using Microsoft.FSharp.Core;
namespace FSharpParts
@akimboyko
akimboyko / ActivePattern01.fsx
Last active August 29, 2015 14:10
Active Pattern sample from F#, THIS AINT A GAME by ANDREA MAGNORSKY http://www.roundcrisis.com/presentations/fsharp_this_aint_a_game/index.html#/
let (|SpaceKey|) (keyboard:KeyboardInput) =
keyboard.KeyPressed(Key.Space)
let (|Hold100ms|) (keyboard:KeyboardInput) =
keyboard.KeyPressedFor(Key.I, 100)
match DualityApp.Keyboard with
| SpaceKey true & Hold100ms false -> playerGo Jump
| SpaceKey true & Hold100ms true -> playerGo DoubleJump
@akimboyko
akimboyko / 01_SayHello.fsx
Last active May 28, 2023 13:00
Samples from "Actor-based Concurrency with F# and Akka.NET" http://bit.ly/FSharpAkkaNET
#time "on"
#load "Bootstrap.fsx"
open System
open Akka.Actor
open Akka.Configuration
open Akka.FSharp
open Akka.TestKit
// #Using Actor
@akimboyko
akimboyko / !Results.md
Created December 1, 2014 06:46
Comparing executing time using Pseq and ParStream while preprocessing 1.4GB text file

Using ParStream

  • Real: 00:00:32.700, CPU: 00:02:01.165, GC gen0: 285, gen1: 80, gen2: 5
  • Real: 00:00:31.421, CPU: 00:01:59.621, GC gen0: 234, gen1: 65, gen2: 4
  • Real: 00:00:32.865, CPU: 00:02:02.335, GC gen0: 250, gen1: 70, gen2: 5
  • Average 32328.7ms

Using PSeq

  • Real: 00:00:34.108, CPU: 00:02:06.470, GC gen0: 203, gen1: 58, gen2: 5
  • Real: 00:00:33.386, CPU: 00:02:06.392, GC gen0: 224, gen1: 63, gen2: 5
  • Real: 00:00:33.720, CPU: 00:02:07.764, GC gen0: 218, gen1: 63, gen2: 5
@akimboyko
akimboyko / PointOfSaleTerminal.fs
Last active August 29, 2015 14:07
Terminal Kata
namespace Terminal
open System
open System.Collections.Immutable
[<Measure>] type USD
[<Measure>] type Volume
[<Measure>] type Percent
type Price =
@akimboyko
akimboyko / problem17.fsx
Created May 4, 2014 05:30
Count number of letters written out in words without spaces or hyphens using F# and Humanizer https://github.com/MehdiK/Humanizer
#r @"Humanizer.dll"
open Humanizer
[1 .. 1000] |>
Seq.map (fun n -> Humanizer.NumberToWordsExtension.ToWords(n)) |>
Seq.collect(fun writtenOutInWords -> writtenOutInWords.ToCharArray()) |>
Seq.where(fun ch -> ch <> ' ') |>
Seq.where(fun ch -> ch <> '-') |>
Seq.length
@akimboyko
akimboyko / FsCheckSamples.fsx
Last active March 27, 2019 08:04
Samples for Property-based testing using FsCheck for F# and C# talk for Kiev Alt.NET
#I @".\packages\FsCheck.0.9.2.0\lib\net40-Client\"
#r @"FsCheck.dll"
// #time "on"
open FsCheck
// simple example
let revRevIsOrig (xs:list<int>) = List.rev(List.rev xs) = xs
Check.Quick revRevIsOrig;;

Toy Problem: Missionaries and Cannibals

Description

On one bank of a river are three missionaries (black triangles) and three cannibals (red circles). There is one boat available that can hold up to two people and that they would like to use to cross the river. If the cannibals ever outnumber the missionaries on either of the river’s banks, the missionaries will get eaten. How can the boat be used to safely carry all the missionaries and cannibals across the river?

Task

Try to implement the general search algorithm just described. You can use LIFO and FIFO as queuing strategies to determine the order in which nodes are explored. These two strategies are known as depth-first and breadth-first search respectively. Be careful, depth-first search may descend down infinite branches, so best implement a depth cut-off. Then, extend your implementation with a hash table that stores all the nodes found so far. Print out a trace of the states the algorithm finds (in the order they are discovered) and see ho

public static class EnumerableEx
{
public static IEnumerable<R> Select<T1, T2, R>(this IEnumerable<Tuple<T1, T2>> source, Func<T1, T2, R> f)
{
return source.Select(t => f(t.Item1, t.Item2));
}
}
Enumerable.Range(1, 10)
.Select(x => Tuple.Create(x, x))
@akimboyko
akimboyko / DI_patterns.fs
Created January 12, 2014 13:24
DI patterns from Scala adopted to F#: structural typing and currying
// Inspired by Scala and http://skov-boisen.dk/?p=289
type Cell() =
override m.ToString() = "*"
type IGameOfLife =
abstract member Next: IGameOfLife
abstract member Generation : seq<Cell>
// Types for Structural Typing