Skip to content

Instantly share code, notes, and snippets.

@explicite
explicite / CellularAutomaton.java
Created April 7, 2012 11:09
Cellular Automaton 1D - AGH
import java.util.concurrent.TimeUnit;
/**
* Implementation the simplest class of one-dimensional cellular automata. Elementary cellular automata have two
* possible values for each cell (0 or 1), and rules that depend only on nearest neighbor values. As a result,
* the evolution of an elementary cellular automaton can completely be described by a table specifying the state
* a given cell will have in the next generation based on the value of the cell to its left, the value the cell itself,
* and the value of the cell to its right.
*
* @author Jan Paw
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import java.util.List;
/**
* @author Jan Paw
* Date: 23.07.13
import java.util.List;
/**
* User service interface
*
* @author Jan Paw
* Date: 23.07.13
*/
public interface IUserService {
public User getById(long id);
import scala.actors.Actor
object Calculator extends Actor {
var v: Int = 0
this.start()
override def act(): Unit = {
loop {
react {
case Inc(i) => v += i
import Calculator.{Get, Dec, Inc}
import scala.actors.Actor
object Client extends Actor {
override def act(): Unit = {
Calculator ! Inc(4)
Calculator ! Inc(2)
Calculator ! Dec(1)
Calculator ! Get()
def !!!(msg: String, e: Throwable) = throw new Exception(msg, e)
implicit class ExceptionEnricher[A](thingToDo: => A) {
def !!!!(msg: String): A = {
try {
thingToDo
} catch {
case e: Throwable =>
!!!(msg, e)
trait |[+A, +B] extends Dynamic {
type Union[T] = T =|= (A | B)
}
let rk4 h f (x, y) =
let k1 = h * f(x, y)
let k2 = h * f(x + 0.5*h, y + 0.5*k1)
let k3 = h * f(x + 0.5*h, y + 0.5*k2)
let k4 = h * f(x + h, y + k3)
x + h, y + k1 / 6.0 + k2 / 3.0 + k3 / 3.0 + k4 / 6.0
// A single step
rk4 1.0 (fun (x, y) -> y) (0.0, 1.0)
let solve n r f =
let dx = 1.0 / float(n-1)
let dt = r * dx * dx
let g u0 u1 u2 = r*u0 + (1.0 - 2.0 * r)*u1 + r*u2
( 0.0,
[|for i in 0..n-1 ->
f(float i / float(n-1))|] )
|> Seq.unfold (fun (t, us) ->
Some( (t, us),
(t + dt,
class Cached[-I, +O](f: I => O) extends (I => O) {
private[this] val cache = scala.collection.concurrent.TrieMap.empty[I, O]
def apply(x: I): O = {
if (cache.contains(x)) {
cache(x)
} else {
val y = f(x)
cache += (x -> y)
y