Skip to content

Instantly share code, notes, and snippets.

View lihaoyi's full-sized avatar

Li Haoyi lihaoyi

View GitHub Profile
object ScalaJSExample extends js.JSApp{
def main() = {
val xs = Seq(1, 2, 3)
println(xs.toString)
val ys = Seq(4, 5, 6)
println(ys.toString)
val zs = for{
x <- xs
y <- ys
} yield x * y
@lihaoyi
lihaoyi / gist:1ebdd89b065a741a1ea72b5d780b9ab1
Last active January 2, 2017 20:01
Setting up a "Walk through pre-loaded example snippets every time you press spacebar" experience in the Ammonite REPL
lihaoyi Ammonite$ amm
Loading...
Welcome to the Ammonite Repl 0.8.0
(Scala 2.11.8 Java 1.8.0_112)
@ {
val funnyFilter = ammonite.terminal.Filter.simple(" ")((b, c, m) =>
if (FunnyFrontEnd.nextLine == Nil) (b.take(c) ++ " " ++ b.drop(c), c+1)
else {
val head :: tail = FunnyFrontEnd.nextLine
FunnyFrontEnd.nextReader = new java.io.StringReader(head)

PSA: if anyone is wondering to how to add debuggability to your Scalatags fragments to figure out where stuff comes from, you can do it with the SourceCode library (which is already pulled in by Scalatags):

  def tagged(implicit name: sourcecode.Enclosing, line: sourcecode.Line) =
    data.enclosing := (name.value + ":" + line.value)

Use e.g.

val searchForm = form(
 tagged,
package test
package grepgit.core
import scala.collection.mutable
import scala.reflect.ClassTag
/**
* A Generator of elements of type [[A]].
*
* [[Generator]] is basically the inverse of
/**
* React v15.3.0
*
* Copyright 2013-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
val x = 1
import scalatags.Text.all._
import collection.mutable
// http://flatuicolors.com/
val red = "#c0392b"
val green = "#27ae60"
val yellow = "#f39c12"
val blue = "#2980b9"
val magenta = "#8e44ad"
val cyan = "#16a085"
val black = "#000"
import java.io.File
import org.jsoup.Jsoup
import org.jsoup.nodes.Document
import scala.io.Source
import collection.mutable
case class Decl(val index: Int,
val cls: String,
val name: String,
haoyi-mbp:test haoyi$ cat Server.scala
load.ivy("com.typesafe.play" %% "play" % "2.5.0")
load.ivy("com.typesafe.play" %% "play-netty-server" % "2.5.0")
load.ivy("org.scalaj" %% "scalaj-http" % "2.2.1")
@
import play.core.server._, play.api.routing.sird._, play.api.mvc._
// Workaround for #371
Thread.currentThread.setContextClassLoader(getClass.getClassLoader)
val config = play.api.Configuration(
@lihaoyi
lihaoyi / Play-Ammonite-Repl.scala
Created April 1, 2016 12:38
Running a PlayFramework server, right in the REPL!
haoyi-mbp:test haoyi$ ~/amm
Loading...
Welcome to the Ammonite Repl 0.5.7
(Scala 2.11.8 Java 1.8.0_25)
haoyi-test@ load.ivy("com.typesafe.play" %% "play" % "2.5.0")
haoyi-test@ load.ivy("com.typesafe.play" %% "play-netty-server" % "2.5.0")
haoyi-test@ import play.core.server._, play.api.routing.sird._, play.api.mvc._
import play.core.server._, play.api.routing.sird._, play.api.mvc._