Skip to content

Instantly share code, notes, and snippets.

View akimboyko's full-sized avatar
🙃

Akim Boyko akimboyko

🙃
View GitHub Profile
@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
@akimboyko
akimboyko / quotes.txt
Last active January 5, 2016 13:57 — forked from mausch/gist:8227399
Quotes by Erik Meijer from Reactive programming course
"Hopefully the third answer is right; but who knows, maybe I made a mistake; I’m just a human, I can throw exceptions as well."
"I am waving my hands on purpose here, this is very spaghetti like code. And spaghetti is great as food, but not good as code."
"flatMap will allow us to focus on the happy path. flatMap will take care of all the noise. flatMap is the dolby for programmers."
"Great programmers write baby code"
"it's obviously correct"
@akimboyko
akimboyko / BinaryTreeCheck.scala
Created December 22, 2013 14:08
Properties proved by ScalaCheck about BinaryTree from Reactive course week #005
package actorbintree
import scala.concurrent.duration._
import akka.actor.{Props, ActorSystem}
import akka.testkit._
import org.scalacheck.{Gen, Properties}
import org.scalacheck.Prop._
import actorbintree.BinaryTreeSet._
import actorbintree.BinaryTreeSet.Contains
import actorbintree.BinaryTreeSet.OperationFinished
@akimboyko
akimboyko / GameOfLife.scala
Created December 15, 2013 13:13
Game of Life implemented on Scala during Global Day of Coderetreat 2013 at Kiev
package main.scala.com.coderetreat
object GameOfLife {
// data structure
class Cell(x: Int, y: Int) {
val posX = x
val posY = y
override def toString = "Cell: " + x + "," + y
@akimboyko
akimboyko / 1readme.md
Created December 5, 2013 04:23
Why is Future.always useful? Why would I use it if its value is already precomputed?

Future.always is a way to lift a normal value T to a Future[T] value. This lifting pattern is something you will see often in functional programming, so remember it well!

To make its usefulness more apparent - imagine that your API method should either call some Web service or look in the cached responses to see if the Web service was already queried with that request. In the first case you have to return a future Future[String] of a response, and in the second you need to return a response String right away from your cache. Future.always can help you solve this tricky type situation.