This file contains 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 CommandLineDemo extends App { | |
val clargs: CLArgs = CLArgs(args) | |
val all: Option[Unit] = clargs.flag('a', "all") | |
val list: Option[Unit] = clargs.shortFlag('l') | |
val ctl: Option[Unit] = clargs.longFlag("show-control-chars") | |
val tab: Option[String] = clargs.value('T', "tabsize") | |
println(s"$all-$list-$ctl-$tab") |
This file contains 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 scala.collection.generic.CanBuildFrom | |
import scala.collection.{TraversableLike, mutable} | |
import scala.reflect.ClassTag | |
object EitherMonad { | |
trait Error[E] extends ClassTag[E] { | |
def getDefault: E = fromMessage("") | |
def fromMessage(msg: String): E | |
def fromThrowable(err: Throwable): E = fromMessage(err.toString) |
This file contains 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
interface Pet { | |
left(); | |
right(); | |
} | |
class Dog extends Pet { | |
left() { bark(); } | |
right() { run(); } | |
bark() { ... } |
This file contains 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
// Prerequisits: | |
// - pattern matching (first half of Ch. 4 in LYAHFGG), | |
// - tail recursion vs non-tail recursion (first half of | |
// Ch. 1 of SICP). | |
// 1. Clearly and unambiguously define the problem | |
// (including the symbol `List'). | |
sealed abstract class List[A] | |
case class Empty[A]() extends List[A] |
This file contains 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
type Query { | |
allPersons(last: Int): [Person!]! | |
allPosts(last: Int): [Post!]! | |
} | |
type Mutation { | |
createPerson(name: String!, age: String!): Person! | |
updatePerson(id: ID!, name: String!, age: String!): Person! | |
deletePerson(id: ID!): Person! | |
createPost(title: String!): Post! |
This file contains 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 java.nio.charset.StandardCharsets.UTF_8 | |
import java.nio.file.{Path, Paths} | |
import java.time.{Duration, Instant} | |
object Lib { | |
def app( | |
filename: String, | |
getTime: Unit => Instant, | |
readFile: Path => Array[Byte], |
This file contains 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 TailRecursiveTreeFold { | |
sealed trait Tree[A] | |
case class Leaf[A](value: A) extends Tree[A] | |
case class Branch[A](left: Tree[A], right: Tree[A]) extends Tree[A] | |
def fold[A, B](tree: Tree[A], | |
f: A => B, | |
g: (B, B) => B): B = { |
This file contains 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 math | |
def f(T,Y): | |
"""An example first-order ODE, in the form y' = f(t,y). | |
Arguments: | |
T -- the independent variable | |
Y -- the dependent variable | |
""" | |
return math.exp(T)*math.sin(Y) |
This file contains 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
module EulersMethod where | |
type R = Double | |
type ODE = (R, R) -> R | |
next :: ODE -> R -> (R, R) -> (R, R) | |
next y' dx (x, y) = (x + dx, y + y' (x, y) * dx) | |
eulers :: ODE -> R -> (R, R) -> [(R, R)] | |
eulers y' dx (x, y) = (x, y) : eulers y' dx (next y' dx (x, y)) |
This file contains 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
module EulersMethod where | |
type R = Double | |
type ODE = (R, R) -> R | |
---- | |
-- Direct Approach | |
---- | |
euler1 :: ODE -> (R, R) -> R -> Int -> [(R, R)] |