Skip to content

Instantly share code, notes, and snippets.

View guizmaii's full-sized avatar
🏄‍♂️

Jules Ivanic guizmaii

🏄‍♂️
View GitHub Profile
@guizmaii
guizmaii / latency.markdown
Created January 16, 2019 12:19 — forked from hellerbarde/latency.markdown
Latency numbers every programmer should know

Latency numbers every programmer should know

L1 cache reference ......................... 0.5 ns
Branch mispredict ............................ 5 ns
L2 cache reference ........................... 7 ns
Mutex lock/unlock ........................... 25 ns
Main memory reference ...................... 100 ns             
Compress 1K bytes with Zippy ............. 3,000 ns  =   3 µs
Send 2K bytes over 1 Gbps network ....... 20,000 ns  =  20 µs
SSD random read ........................ 150,000 ns  = 150 µs

Read 1 MB sequentially from memory ..... 250,000 ns = 250 µs

// Alternative to sealed abstract case class pattern for Scala 2.12.2+
// Benefits:
// - 1 final class instead of 1 sealed class + anonymous subclass
// - portable to Scala 3 regardless of opaque types
// - less boilerplate
final case class Angle private (toDegrees: Int) {
// Define our own `copy` method to suppress synthetic one
// Add private to prevent it from being used
def copy(degrees: Int = toDegrees): Angle = Angle.fromDegrees(degrees)
@guizmaii
guizmaii / nel_serialize.md
Created December 17, 2018 16:09 — forked from ferhtaydn/nel_serialize.md
How to serialize a model class has NonEmptyList attributes like normal List

How to use NonEmptyList in a model class

Let's say you are implementing an API and decide to return a base error response in common error situations such as BadRequest, NotFound etc.

final case class Error(code: String, description: String)

If you are returning an error response, your assumption would probably be that such responses should contain at least one error.

@guizmaii
guizmaii / ast.txt
Created December 10, 2018 21:02 — forked from enebo/ast.txt
jruby -S ast -e 'i += 1; i = i + 1'
AST:
RootNode 0
BlockNode 0
LocalAsgnNode:i 0
CallNode:+ 0
LocalVarNode:i 0
ArrayNode 0
FixnumNode 0
, null
@guizmaii
guizmaii / each_benchmark.rb
Created December 10, 2018 19:59 — forked from jodosha/each_benchmark.rb
Ruby benchmark: Array#each vs for x in array
#!/usr/bin/env ruby -w
require "benchmark"
TIMES = 100_000
ARRAY = (1..1_000).to_a
Benchmark.bm(30) do |b|
b.report "each" do
TIMES.times do |i|
ARRAY.each do |element|
@guizmaii
guizmaii / stats_logger.rb
Created November 14, 2018 16:45 — forked from LeZuse/stats_logger.rb
Puma plugin for stats logging on Heroku
Puma::Plugin.create do
def production?
ENV.fetch('RACK_ENV', 'development') == 'production'
end
def log(msg)
if production?
Rails.logger.info msg
else
puts msg

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
@guizmaii
guizmaii / shared-state-in-fp.md
Created June 2, 2018 11:26 — forked from gvolpe/shared-state-in-fp.md
Shared State in pure Functional Programming

Shared State in pure Functional Programming

Newcomers to Functional Programming are often very confused about the proper way to share state without breaking purity and end up having a mix of pure and impure code that defeats the purpose of having pure FP code in the first place.

Reason why I decided to write up a beginner friendly guide :)

Use Case

We have a program that runs three computations at the same time and updates the internal state to keep track of the

@guizmaii
guizmaii / x.scala
Last active May 28, 2018 16:28 — forked from kevinmeredith/x.scala
Run IO[List[Int]] in Parallel using Ammonite
@ import $ivy.`org.typelevel::cats-effect:0.10.1`
@ import cats._, cats.data._, cats.implicits._, cats.effect._, scala.concurrent.duration._, import scala.concurrent.ExecutionContext.Implicits.global
@ def lift(i: Int): IO[Int] = Timer[IO].sleep(1.second) >> IO {
println(Thread.currentThread.getName + "At time = " + java.time.Instant.now)
i
}
@guizmaii
guizmaii / x.scala
Created May 28, 2018 16:24 — forked from kevinmeredith/x.scala
Run IO[List[Int]] in Parallel using Ammonite
@ import $ivy.`org.typelevel::cats-effect:0.10.1`
@ import cats._, cats.data._, cats.implicits._, cats.effect._
@ def lift(i: Int): IO[Int] = Timer[IO].sleep(1.second) >> IO {
println(Thread.currentThread.getName + "At time = " + java.time.Instant.now)
i
}