Skip to content

Instantly share code, notes, and snippets.

@viktorklang
viktorklang / minscalaactors.scala
Last active March 25, 2024 19:01
Minimalist Scala Actors
/*
Copyright 2012-2021 Viktor Klang
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
@retronym
retronym / implicitly-either.scala
Created April 20, 2012 22:24
implicitly-either
scala> def implicitlyEitherImpl[A: c.TypeTag, B: c.TypeTag](c: reflect.makro.Context): c.Expr[Either[A, B]] = {
| def i[X](implicit X: c.TypeTag[X]): c.Tree = c.inferImplicitValue(X.tpe, silent = false)
| i[A] match {
| case c.mirror.EmptyTree =>
| i[B] match {
| case c.mirror.EmptyTree =>
| sys.error("Neither A nor B are available implicitly")
| case b =>
| c.reify(Right[A, B](c.Expr[B](b).eval))
| }
@iron9light
iron9light / MixedTypeArrayJsonSuite.scala
Created July 19, 2012 05:51
lift json serializing mixed type array
package net.liftweb.sandbox
import org.scalatest.FunSuite
import net.liftweb.json._
import org.scalatest.matchers.ShouldMatchers
/**
* @author IL
* @see <a href="https://groups.google.com/forum/?fromgroups#!topic/liftweb/GK5DbzCtWdA">forum</a>
*/
@dcbriccetti
dcbriccetti / WeightedQuickUnion.scala
Created August 13, 2012 05:06
A Scala version of the weighted quick-union algorithm from Sedgewick’s Coursera Algorithms class
val unions = Seq(4->3, 3->8, 6->5, 9->4, 2->1, 5->0, 7->2, 6->1, 1->0)
val parents = (0 to 9).toArray
val depths = Array.fill(10)(1)
def connected(p: Int, q: Int) = root(p) == root(q)
def union(p: Int, q: Int) {
val i = root(p)
val j = root(q)
val (sm, lg) = if (depths(i) < depths(j)) (i, j) else (j, i)
@ymasory
ymasory / gist:3782247
Created September 25, 2012 14:24
Build Your Website in Scala in a Day

Build Your Website in Scala in a Day

This is a proposed event to be held in Philly. Comments welcome.

  • Theme is "Build Your Website in Scala in a Day". The goal would be for each participant to literally create and deploy a website in a Scala web framework in one day. No spectators; everyone builds a site! It can be your personal homepage, a hobby site, a business site, or anything else, so long as you fully intend to deploy (in alpha condition) at the end of the day.
  • The event would be oriented toward developers who already have a reasonable knowledge of Scala, but have never used it to build a website.
  • The event would be held on a Friday in 6-8 weeks, in Philadelphia. Friday because I want it to be a little more professional and business-oriented than Scalathon. Employers who actually need a website can send employees, for example.
  • It would feature 2 or 3 frameworks (to be determined, based on interest/availability of core developers to attend). There would be maybe 7-10 participants p
@viktorklang
viktorklang / NonblockingCache.scala
Last active December 11, 2015 23:38
Nonblocking cache
/*©2013 Viktor Klang*/
package akka.util
import java.util.concurrent.atomic.AtomicReference
import scala.concurrent.{ Future, ExecutionContext }
import scala.annotation.tailrec
class Cache[K, V](__ec: ExecutionContext, throughput: Int) extends AtomicReference[Map[K, V]] {
implicit val executor = SerializedSuspendableExecutionContext(throughput)(__ec)
@tailrec final def update(f: Map[K, V] ⇒ Map[K, V]): Map[K, V] = {
val v = get
val nv = f(v)
@tgpfeiffer
tgpfeiffer / EndlessList.scala
Last active October 3, 2016 15:25
AJAX loading of new items in Lift (with Non-AJAX fallback)
class EndlessList {
/**
* Gets a "page" (e.g., 10 items) of content entities from the database.
*/
def getPage(page: Int): (Seq[Content], Boolean) = {
val realPage: Int = if (page < 1) 1 else page
val limit = 10
val contents = Content.findAll(
(/* conditions */),
@eltimn
eltimn / Build.scala
Last active December 22, 2015 12:49
SBT build with deploy.
import sbt._
import sbt.Keys._
object MyBuild extends Build {
import Dependencies._
import BuildSettings._
lazy val main = Project("root", file("."))
.settings(mainWebSettings: _*)
.settings(
@cam-stitt
cam-stitt / FlightComponent.sublime-snippet
Last active December 25, 2015 17:59
Sublime Text snippet for coffee script flight component
<snippet>
<content><![CDATA[
define(function(require) {
var defineComponent = require('flight/lib/component');
return defineComponent(${1});
function ${1}() {
this.defaultAttrs({
@dadoonet
dadoonet / bluekiwi.sh
Created November 8, 2013 17:21
Bluekiwi example
curl -XDELETE "http://localhost:9200/bluekiwi"; echo
curl -XPUT "http://localhost:9200/bluekiwi/doc/1" -d'
{
"group":1,
"title":"test",
"content":"nothing",
"tags":["tag1","tag2"],
"authorId":1,
"creatorId":1