This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//n → n/2 (n is even) | |
//n → 3n + 1 (n is odd) | |
let collatzSeq = Seq.unfold (fun n -> match n with | |
| 0L -> None | |
| 1L -> Some(1L, 0L) | |
| n -> Some(n, if n % 2L = 0L then n/2L else 3L*n+1L) ) | |
let r = {1L..999999L} |> Seq.map(fun i -> (i, Seq.length(collatzSeq(i)))) |> Seq.maxBy(fun (i,s) -> s) | |
printfn "%A" r |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let factors number = seq { | |
for divisor in 1 .. (float >> sqrt >> int) number do | |
if number % divisor = 0 then | |
yield divisor | |
if number <> 1 then yield number / divisor //special case condition: when number=1 then divisor=(number/divisor), so don't repeat it | |
} | |
let rec triangle n = match n with | |
| 0 -> 0 | |
| 1 -> 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val foo = (n: Int) => n.toString | |
val bar = (s: String) => s.toInt | |
// composing | |
val f0 = bar compose foo | |
println(f0(3)) | |
//what if the functions could fail.. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
case class PermutingCheckout(skuRules: Map[String, SKUPricer]) extends TotalCalculator { | |
// as above but runs the rules in all permuted orders to find the best order to minimise the price | |
val calculateTotal = (items: List[String]) => | |
items.foldMap(x => Map(x -> 1)) // create a map of SKU -> count of 'scanned' items | |
.map { case (sku, skuCount) => | |
// get the rules for the SKU (if they exist) and map them to get the results for each SKU group | |
skuRules.get(sku).map(rule => (rule.getPrice(skuCount)).success) | |
.getOrElse(s"'$sku' rule not found".failureNel) | |
}.reduce(_ |+| _) // sum the results for each SKU to get the final total | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
case class ResultA() | |
case class ResultB() | |
case class ResultC() | |
def test(): Future[Either[ResultA, ResultB]] = ??? | |
type Results = ResultA :+: ResultB :+: ResultC :+: CNil | |
val foo = for { | |
x <- test() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.ensime.json.FamilyFormats | |
import spray.json._ | |
object MainApp extends App { | |
case class Foo(blah: String, blah2: Option[String]) | |
case class Bar(boo: List[Foo]) | |
val wat = Bar(Foo("dfsdf", Some("dsfd")) :: Foo("feefef", None) :: Nil) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import shapeless._ | |
import spray.json._ | |
object Main extends App { | |
case class Test(optionalBlah: Option[String]) | |
case class Test2(nested: Test) | |
import DefaultJsonProtocol._ | |
implicit val testFormat = DefaultJsonProtocol.jsonFormat1(Test) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object ScalaJSExample extends js.JSApp{ | |
def main() = { | |
val xs = Seq(1, 2, 3) | |
println(xs.toString) | |
val ys = Seq(4, 5, 6) | |
println(ys.toString) | |
val zs = for{ | |
x <- xs | |
y <- ys | |
} yield x * y |
NewerOlder