Skip to content

Instantly share code, notes, and snippets.

"U2" has a concert that starts in 17 minutes and they must all
cross a bridge to get there. They stand on the same side of the
bridge. It is night. There is one flashlight. A maximum of two
people can cross at one time, and they must have the flashlight
with them. The flashlight must be walked back and forth. A
pair walk together at the rate of the slower man's pace:
Bono 1 minute to cross
Edge 2 minutes to cross
Adam 5 minutes to cross
@frgomes
frgomes / ImplicitExtensionMethods.scala
Last active April 4, 2018 12:23
Scala - implicit extension methods (pimp my library) using implict value classes
//------------------------------------------------------------------------
// Implicit Extension Methods a.k.a. Pimp My Library
//
// In a nutshell, this is about avoiding create instances of implicit
// classes which will live very shortly and later will be garbage
// collected, hurting hard performance in certain circumstances.
//
// More info:
// http://docs.scala-lang.org/sips/completed/value-classes.html
// http://docs.scala-lang.org/sips/completed/implicit-classes.html
object game {
case class Lens[S, A](set: A => S => S, get: S => A) { self =>
def >>> [B](that: Lens[A, B]): Lens[S, B] =
Lens[S, B](
set = (b: B) => (s: S) => self.set(that.set(b)(self.get(s)))(s),
get = (s: S) => that.get(self.get(s))
)
}
case class Prism[S, A](set: A => S, get: S => Option[A]) { self =>
@lossyrob
lossyrob / write-one-line-text.scala
Last active August 14, 2019 07:54
Read \ Write a text file in one line Scala
def write(path: String, txt: String): Unit = {
import java.nio.file.{Paths, Files}
import java.nio.charset.StandardCharsets
Files.write(Paths.get(path), txt.getBytes(StandardCharsets.UTF_8))
}
def read(path: String): String =
scala.io.Source.fromFile(path, "UTF-8").getLines.mkString
@banjeremy
banjeremy / load-resource.scala
Created January 29, 2017 00:18
scala: load files from resources directory
def loadResource(filename: String) = {
val source = scala.io.Source.fromURL(getClass.getResource(filename))
try source.mkString finally source.close()
}
@rafaotetra
rafaotetra / LC_CTYPE.txt
Created January 8, 2018 11:54 — forked from thanksdanny/LC_CTYPE.txt
Centos warning: setlocale: LC_CTYPE: cannot change locale (UTF-8): No such file or directory
vi /etc/environment
add these lines...
LANG=en_US.utf-8
LC_ALL=en_US.utf-8
@kabinpokhrel
kabinpokhrel / server.py
Created December 22, 2017 13:35
Simple HTTP Server Implementation in Python 3
import http.server as httpserver
import socketserver
def main(port=None):
if port is None:
port = 8000
handler = httpserver.SimpleHTTPRequestHandler
try:
httpd = socketserver.TCPServer(("", port), handler)
print("serving at port", port)
@enjalot
enjalot / cors_server.py
Created June 10, 2012 06:19
Allow CORS with python simple http server
import SimpleHTTPServer
class CORSHTTPRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
def send_head(self):
"""Common code for GET and HEAD commands.
This sends the response code and MIME headers.
Return value is either a file object (which has to be copied
to the outputfile by the caller unless the command was HEAD,
and must be closed by the caller under all circumstances), or
@navicore
navicore / stdDev.scala.md
Created December 11, 2017 16:47
standard deviation scala
import Numeric.Implicits._

def mean[T: Numeric](xs: Iterable[T]): Double = xs.sum.toDouble / xs.size

def variance[T: Numeric](xs: Iterable[T]): Double = {
  val avg = mean(xs)

  xs.map(_.toDouble).map(a => math.pow(a - avg, 2)).sum / xs.size
}
@sloanlance
sloanlance / BASH: ISO 8601 Timestamp with Milliseconds
Last active February 9, 2023 19:33
How to get an ISO 8601 timestamp with milliseconds in BASH
Gist title: "BASH: ISO 8601 Timestamp with Milliseconds"
Summary: How to get an ISO 8601 timestamp with milliseconds in BASH