Skip to content

Instantly share code, notes, and snippets.

@ms-tg
ms-tg / why-doubles.scala
Last active August 29, 2015 14:26
In response to Slack post "Please explain why Doubles work this way"
// In response to Slack post "Please explain why Doubles work this way"
//
// For doubles...
// 1.001484375 - 1 = 0.0014843749999999822
// 0.001484375 + 1 = 1.001484375
//
// To run, start the repl with `sbt console`, then paste all this in
//
import java.lang.Double.doubleToRawLongBits
import java.lang.Long.{toBinaryString, toHexString}
@ms-tg
ms-tg / sbt-tg.sh
Last active August 29, 2015 14:04
Workaround to launch SBT when `play` command from 2.0.8 is also used
#!/bin/bash
# WORKAROUND (2014-07-16, Marc): Avoids "No scala version specified or detected"
# error when Play 2.0.x and 2.3.x projects are both built by same local user.
if [ -e "${HOME}/.sbt/boot/other" ]; then
rm -rfv ~/.sbt/boot/other
rm -v ~/.ivy2/cache/org.scala-sbt/sbt/ivy-0.13.5.xml
fi
export SBT_OPTS="-Dsbt.repository.config=/etc/sbt/repositories -Dsbt.override.build.repos=true"
@ms-tg
ms-tg / jdk8_optional_monad_laws.java
Created November 11, 2013 21:14
Does JDK8's Optional class satisfy the Monad laws? Yes, it does.
/**
* ```
* Does JDK8's Optional class satisfy the Monad laws?
* =================================================
* 1. Left identity: true
* 2. Right identity: true
* 3. Associativity: true
*
* Yes, it does.
* ```
@ms-tg
ms-tg / scala_try_monad_axioms.scala
Last active May 28, 2022 03:48
Scala Try: monad axioms
import scala.util.{Try, Success, Failure}
def f(s: String): Try[Int] = Try { s.toInt }
def g(i: Int): Try[Int] = Try { i * 2 }
def unit[T](v: T): Try[T] = Success(v)
//val v = "1"
val v = "bad"
val m = Success(v)
@ms-tg
ms-tg / for-comp-either-right.scala
Created August 6, 2013 17:07
From Ratan: desugar a for-comprehension to see why assignment won't work with Either RightProjections
////
// Output of running:
// $ scala -Xprint:parser -e "val e: Either[String, Int] = Right(1); for {i <- e.right; j = i + 1} yield j"
////
//
// Picked up _JAVA_OPTIONS: -Xss3m -XX:MaxPermSize=256M
// /tmp/scalacmd2064075741948415763.scala:1: error: value map is not a member
// of Product with Either[String,(Int, Int)] with Serializable
// val e: Either[String, Int] = Right(1); for {i <- e.right; j = i + 1} yield j
// ^
@ms-tg
ms-tg / test-dpkg-version-naming.bash
Last active December 20, 2015 10:30
Test our package naming proposal for debian
#!/bin/bash
##
# A script to test our debian package naming policy.
#
# This script uses `dpkg --compare-versions` to test that provided
# sequences of package names are considered to be in ascending order
# by Debian's tools.
#
# Author: Marc Siegel <marc.siegel@timgroup.com>
# Modified: 2013-07-30
@ms-tg
ms-tg / StringWithToIntOpt.scala
Created March 18, 2013 14:38
Example of Scala enrich-my-library pattern: adding String#toIntOpt
package enriching_example
import scala.util.control.Exception.allCatch
case class StringWithToInt(s: String) {
def toIntOpt: Option[Int] = allCatch opt { s.toInt }
}
implicit def string2stringWithToInt(s: String) = StringWithToInt(s)
CREATE TABLE filler (
id INT NOT NULL PRIMARY KEY AUTO_INCREMENT
) ENGINE=Memory;
CREATE TABLE t_param (
param INT NOT NULL PRIMARY KEY
) ENGINE=Memory;
CREATE TABLE t_source (
id INT NOT NULL PRIMARY KEY,
require 'hamster'
require 'hamster/experimental/mutable_hash'
# a bunch of threads with a read/write ratio of 10:1
num_threads = 100
num_reads_per_write = 10
num_loops = 500
hsh = Hamster.mutable_hash
puts RUBY_DESCRIPTION
@ms-tg
ms-tg / gist-1.rb
Created October 18, 2012 18:13 — forked from rondale-sc/gist-1.rb
try_and_try_again
class Project
attr_reader :supervisor
# assume supervisor looks like:
# ["Harry", "Henderson", "hhendersonfake@me.com"]
def initialize(supervisor)
@supervisor = supervisor
end