Skip to content

Instantly share code, notes, and snippets.

View Vilkina's full-sized avatar
❤️
I may be slow to respond.

Aleksandra A Vilkina

❤️
I may be slow to respond.
View GitHub Profile

Applied Functional Programming with Scala - Notes

Copyright © 2016-2018 Fantasyland Institute of Learning. All rights reserved.

1. Mastering Functions

A function is a mapping from one set, called a domain, to another set, called the codomain. A function associates every element in the domain with exactly one element in the codomain. In Scala, both domain and codomain are types.

val square : Int => Int = x => x * x
def zipAllWith[@specialized B, @specialized C](that: Chunk[B])(left: A => C, right: B => C)(both: (A, B) => C): Chunk[C] = {
val size = self.length.max(that.length)
val minln = self.length.min(that.length)
if (size == 0 && minln == 0) Chunk.empty
else {
var j = 0
val c = both(self(j), that(j))
implicit val C: ClassTag[C] = Chunk.Tags.fromValue(c)
package ziolist
import zio.{ ZEnv, ZIO }
import zio._
sealed trait MyList[+ A] {
def head: Option[A]
def tail: MyList[A]
def isEmpty: Boolean
def add[B >: A](element: B): MyList[B]
package ziolist.http4s
import org.http4s._
import zio._
import zio.interop.catz._
import org.http4s.server.blaze.BlazeServerBuilder
object Service {
//E for Effect
package com.oleksandrah.cakes
import com.oleksandrah.cakes.model.{Cake, Cakes, Tables}
import org.scalatra.ScalatraServlet
import org.scalatra.scalate.ScalateSupport
import slick.jdbc.H2Profile
import slick.jdbc.H2Profile.api._
import zio.{Exit, IO, Task, ZIO, _}
// Array#update
val array = Array("a", "b", "c")
array.update(0, "z")
def copyChunk(from: AsynchronousFileChannel,
to : AsynchronousFileChannel) =
for {
chunk <- from.read(1024, 0)
_ <- to.write(chunk, 0)
} yield ()
[info] Benchmark (size) Mode Cnt Score Error Units
[info] ArrayBenchmarks.find 1000 avgt 15 40.282 ± 0.109 ns/op
[info] ArrayBenchmarks.findOptimized 1000 avgt 15 7.645 ± 0.002 ns/op
[info] ArrayBenchmarks.flatMap 1000 avgt 15 31442.367 ± 11.125 ns/op
[info] ArrayBenchmarks.flatMapOptimized 1000 avgt 15 14086.659 ± 35.318 ns/op
[info] ArrayBenchmarks.fold 1000 avgt 15 13050.998 ± 6.377 ns/op
[info] ArrayBenchmarks.map 1000 avgt 15 10070.717 ± 15.476 ns/op
[info] ArrayBenchmarks.mapOptimized 1000 avgt 15 461.821 ± 2.040 ns/op
[info] ChunkArrayBenchmarks.find 1000 avgt 15 23.068 ± 0.012 ns/op
[info] ChunkArrayBenchmarks.flatMap 1000 avgt 15 72463.542 ± 111.816 ns/op
package repeat
abstract class MyList[+A] {
def head: A
def tail: MyList[A]
def isEmpty: Boolean
def add[B >: A](element: B): MyList[B]
def printElements: String
override def toString: String = "[" + printElements + "]"
@Vilkina
Vilkina / MyStream.scala
Created July 7, 2020 07:59
Example of implementing stream
package exercises
import scala.annotation.tailrec
abstract class MyStream[+A] {
def isEmpty: Boolean
def head: A
def tail: MyStream[A]