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
}
This file contains 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
"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 |
This file contains 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
//------------------------------------------------------------------------ | |
// 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 |
This file contains 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
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 => |
This file contains 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
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 |
This file contains 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
def loadResource(filename: String) = { | |
val source = scala.io.Source.fromURL(getClass.getResource(filename)) | |
try source.mkString finally source.close() | |
} |
This file contains 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
vi /etc/environment | |
add these lines... | |
LANG=en_US.utf-8 | |
LC_ALL=en_US.utf-8 |
This file contains 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 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) |
This file contains 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 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 |
This file contains 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
Gist title: "BASH: ISO 8601 Timestamp with Milliseconds" | |
Summary: How to get an ISO 8601 timestamp with milliseconds in BASH |
OlderNewer