Skip to content

Instantly share code, notes, and snippets.

@lancewalton
lancewalton / HtmlSelect.scala
Created July 9, 2022 13:59
An HTML select element whose options & selected option are determined by a Laminar Signal of an arbitrary type T, emitting the selected T on a WriteBus
import com.raquo.laminar.api.L.*
import org.scalajs.dom.{HTMLOptionElement, html}
object HTMLSelect {
def signalRequiredSelect[T](options: Signal[List[T]], toValue: T => String, toText: T => String, controlledSelection: Signal[T], userSelection: WriteBus[T]): HtmlElement = {
select(
children <--
options
.split(toValue) { case (v, _, updates) =>
option(
@lancewalton
lancewalton / gist:a56a91e16de9c03715e55bcc903ea120
Last active October 20, 2021 15:41
Problem with Laminar StrictSignal.foldLeft where the accumulated value is an EventStream
import com.raquo.laminar.api.L.*
import munit.FunSuite
import scala.collection.mutable
case class Foo(s: String)
case class Bar(s: String)
object Interpreter {
def interpret(foo: Foo): EventStream[Bar] = EventStream.fromValue(Bar(foo.s))
// Is there a way to do this such that we don't end up with List[Thing[_]],
// but instead end up with something with the types preserved?
object Foo {
sealed trait Thing[T]
case class IntThing(i: Int) extends Thing[Int]
case class StringThing(s: String) extends Thing[String]
case class DoubleThing(d: Double) extends Thing[Double]
// The implementation here is only to get a compile
def differentThingsKnownOnlyAtRuntime(i: Int): Thing[_] = i match {
@lancewalton
lancewalton / FreeBranch.scala
Last active August 23, 2017 17:03
Branching in Free
// Eda and I are using Free. We have a need to handle situations where the right hand side of the for comprehension produces
// an Either and Left means that we need to 'branch' to do something to handle the failure, but handling the failure
// will not result in something that allows the rest of the main program to continue - we still want it to short circuit.
// The branch is itself a Free program, so whatever mechanism is uses needs to put the result of the branch onto the
// Left so that the main program still short circuits.
//
// Question: Is this completely the wrong way to think about this?
//
// If not, I found this: https://gist.github.com/EECOLOR/c312bdf54039a42a3058
//
We need a questionnaire model that can:
* Support different answer types
* Support validations that can depend on the answers to other questions
* Support different forms of rendering (e.g. the questionnaire needs to be renderered using HTML, JSON, etc.) with / without the answers
* Answers need to be persisted and recovered from persistence. JSON persistence is acceptable.
* Support having conditional parts of the questionnaire based on previous answers
Needless to say, it must be typesafe
Different types of answers are things like: Dates, Addresses, Lists of Addresses, Phone Numbers, Lists of Phone Numbers, Strings, Enumerated values, etc.
case class Document(id: String, contentType: String, content: Array[Byte])
object Server {
def apply() = {
val service = HttpService {
case req @ POST -> Root / "document" / id => upload(req, id)
}
JettyBuilder
.bindHttp(8080)
package foo
object Foo extends App {
case class Tree(label: String, children: List[Tree])
trait Show[T] {
def show(t: T): String
}
implicit def listShow[T : Show]() = new Show[List[T]]{
@lancewalton
lancewalton / gource.sh
Last active January 11, 2018 23:37
Produces a video of the evolution of a git repository using gource (see https://code.google.com/p/gource/). Also downloads committers' gravatars to use on the video.
#!/bin/zsh
# For Macs, get gource with HomeBrew:
# brew install gource
# brew install ffmpeg
if (( !($# == 3) ))
then
echo "Usage:"
echo $0 "<gravatar directory> <output file base name> <seconds per day>"
@lancewalton
lancewalton / gist:9983866
Created April 4, 2014 21:56
Example of RX with a feedback loop
import scala.collection.mutable
import rx.lang.scala.{Observable, Subscriber, Subscription}
// This is here to let us construct an Observable that we can 'tell' of new events.
case class SimpleObservable[T](initial: Option[T] = None) {
private val subscribers: mutable.HashSet[Subscriber[T]] = new mutable.HashSet[Subscriber[T]] with mutable.SynchronizedSet[Subscriber[T]]
private var lastValue: Option[T] = initial
val observable = Observable { (subscriber: Subscriber[T]) =>
@lancewalton
lancewalton / Chess
Created August 31, 2013 21:14
Two solutions to the problem of finding all placements of some collection of chess pieces on a chess board of specified size such that no piece is under threat from any other. The problem was recently posed to me by a friend. The 'optimised' solution is significantly faster that the 'unoptimised' one. For example, on my laptop, the n-queens prob…
package chess
// Everything from this line down to the cut is common to the 'unoptimised' and 'optimised' solutions
case class Location(row: Int, column: Int)
trait Piece {
def canAttackLocation(from: Location, to: Location): Boolean
}
case object Rook extends Piece {