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 r = (42, "Hello") | |
val (num, str) = r | |
case class Product(name: String, price: Float) | |
val p = Product("Test", 128.8f) | |
val p2 = p.copy(name = "Another") | |
val p3 = Product("Test", 128.8f) | |
// structural comparision of case classes | |
p == p3 |
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 Circle(radius: Double) { | |
def area = radius * radius * Math.PI | |
} | |
case class Rectangle(side: Double) { | |
def area = side * side | |
} | |
case class Triangle(base: Double, height: Double) { | |
def area = 0.5d * base * height |
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
open System | |
let readConsoleValue prompt = | |
printf "%s: " prompt | |
Console.ReadLine() | |
let classifyAge name age = | |
if age >= 20 then | |
sprintf "%s is no longer a teenager" name | |
elif (age < 20 && age > 13) then |
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
open System | |
let GoldenTuples numbers = | |
let GoldenRatio = (1.0 + Math.Sqrt(5.0)) / 2.0 | |
seq { | |
for n in numbers do | |
yield (n, (float n) * GoldenRatio) | |
} | |
let rec GetNumber numbers = |
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
open System | |
type GunRecord = | |
{ xcoord : float | |
ycoord : float | |
speed : float | |
distance : float | |
name : String } | |
type GunResult = |
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
module DSLFSM where | |
import Data.Char (toLower, toUpper) | |
-- a DSL for basic text processing | |
-- and a FSM for implementing that DSL. | |
-- adapted to Haskell from https://www.youtube.com/watch?v=7D9GE3-o54o | |
data MachineState = Normal | Comment | Upper | Lower |
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
module Chapter11Phone where | |
import Data.Char (isLetter, isNumber, isUpper, toLower) | |
import Data.List (group, sort) | |
data CharType = Letter Char | |
| Number Char | |
| Punctuation Char | |
deriving (Eq, Show) |
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
module Chapter11Tree where | |
data OperatingSystem = Linux | |
| OpenBSD | |
| Mac | |
| Windows | |
deriving (Eq, Show) | |
data ProgrammingLanguage = Haskell | |
| Agda |
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.scalacheck.Gen.choose | |
import org.scalatest.prop.GeneratorDrivenPropertyChecks | |
import org.scalatest.{WordSpec, MustMatchers} | |
class TruncateTest extends WordSpec with MustMatchers with GeneratorDrivenPropertyChecks { | |
def truncate(str: String, length: Int): String = { | |
if (str.length < length) | |
str | |
else if (length < 3 || str.length < 3) |
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
use std::cmp; | |
fn sum(numbers: Vec<i32>) -> i32 { | |
numbers.iter().fold(0, |total, num| total + num) | |
} | |
fn biggest_word(words: Vec<&str>) -> usize { | |
words.iter().fold(0, |length, word| cmp::max(length, word.chars().count())) | |
} |
OlderNewer