Skip to content

Instantly share code, notes, and snippets.

View gustavofranke's full-sized avatar

Gustavo Franke gustavofranke

View GitHub Profile
object Fintech5 {
data class ValidationError(val message: String)
data class CreatePortfolio(val userId: String, val amount: Double)
data class ChangePortfolio(val userId: String, val stock: String, val quantity: Int)
interface Validator<T> {
fun T.check(): EitherNel<ValidationError, T>
}
@gustavofranke
gustavofranke / context-receivers.kt
Last active May 26, 2024 06:11
context receivers
package com.contextReceivers
import kotlin.random.Random
fun String.sarcastic(): String =
asIterable().joinToString("") {
if (Random.nextBoolean()) it.uppercase() else it.lowercase()
}
fun printTransformedGreeting(transform: String.() -> Unit) {
package option
import org.scalatest.funsuite.AnyFunSuite
/**
* show some scala language features that, if you come from other languages, they might be unknown to you.
*
* Like last week, we'll play with the REPL, then turn our findings into tests.
*
* I'll use scala-test to automate this process,
module Geometry.Cube
( volume
, area
) where
import qualified Geometry.Cuboid as Cuboid
volume :: Float -> Float
volume side = Cuboid.volume side side side
Data.Time> :t "foo"
"foo" :: [Char]
Data.Time> :set -XOverloadedStrings
Data.Time> :t "foo" "foo" :: Data.String.IsString p => p
-- stack repl --package <package-name>
-- stack repl --package time
> import Data.Time
Data.Time> getCurrentTime
2020-10-17 15:03:15.521782 UTC
Data.Time> utctDay <$> getCurrentTime
2020-10-17
Data.Time> today <- utctDay <$> getCurrentTime
Data.Time> today
package cipher
def lowers(xs: String): Int =
xs.filter(x => x >= 'a' && x <= 'z').length
def count(x: Char, xs: String): Int =
xs.toList.filter(x0 => x == x0).length
def positions[A](x: A, xs: List[A]): List[Int] =
for
package playground
// https://www.youtube.com/watch?v=xpz4rf1RS8c
// 01. functional effects
// Console - Model 1
sealed trait Console1 { self =>
def +(that: Console1): Console1 = Sequence1(self, that)
}
final case class Print1(line: String) extends Console1
final case class Sequence1(first: Console1, second: Console1) extends Console1
@gustavofranke
gustavofranke / exercises.hs
Last active April 11, 2020 11:19
first chapters sample code
-- 1.7Exercises
-- 1.Give another possible calculation for the result of double (double 2).
double x = x + x
another = (double . double) 2
-- 2.Show that sum [x] = x for any number x.
ex2 :: (Eq a, Num a) => a -> Bool
ex2 x = sum [x] == x
@gustavofranke
gustavofranke / write-you-a-scheme.hs
Created February 17, 2020 12:57
first part of chapter 5
module Main where
import Control.Monad
import Control.Monad.Except
import System.Environment
import Text.ParserCombinators.Parsec hiding (spaces)
main :: IO ()
main = do
args <- getArgs
evaled <- return $ liftM show $ readExpr (args !! 0) >>= eval