Skip to content

Instantly share code, notes, and snippets.

def rpn(str: String): Int = rpnPrime(str.split(' ').toList, Nil)
def rpnPrime(str: List[String], stack: List[Int]): Int = (str, stack) match {
case ("+"::t, y::x::zs) => rpnPrime(t, (x + y) :: zs)
case ("-"::t, y::x::zs) => rpnPrime(t, (x - y) :: zs)
case ("*"::t, y::x::zs) => rpnPrime(t, (x * y) :: zs)
case ("/"::t, y::x::zs) => rpnPrime(t, (x / y) :: zs)
case ("%"::t, y::x::zs) => rpnPrime(t, (x % y) :: zs)
case ( n::t, zs) => rpnPrime(t, (n.toInt) :: zs)
case _ => stack.head
import scala.language.implicitConversions
sealed trait Expr {
def +(that: Expr): Expr = Add(List(this, that))
def *(that: Expr): Expr = Mul(List(this, that))
def +(that: Int ): Expr = this + N( that)
def -(that: Int ): Expr = this + N(-that)
def *(that: Int ): Expr = this * N( that)
import scala.language.implicitConversions
// R is derived from Rational with some fixes.
// https://sites.google.com/site/scalajp/home/documentation/scala-by-example/chapter6
case class R(numer: Int, denom: Int) extends Ordered[R] {
def reduce: R = {
def gcd(x: Int, y: Int): Int = {
if (x == 0) y
else if (x < 0) gcd(-x, y)
import scala.language.implicitConversions
// R is derived from Rational with some fixes.
// https://sites.google.com/site/scalajp/home/documentation/scala-by-example/chapter6
case class R(n: Int, d: Int) extends Ordered[R] {
private def gcd(x: Int, y: Int): Int = {
if (x == 0) y
else if (x < 0) gcd(-x, y)
else if (y < 0) -gcd( x, -y)
sealed trait Expr
case class N(n: Int) extends Expr
case class Var(x: String, a: Int, n: Int) extends Expr
case class Add(n: Expr*) extends Expr
case class Mul(n: Expr*) extends Expr
def x(a: Int, n: Int): Var = {
Var("x", a, n)
}
sealed trait Expr
case class N(n: Int) extends Expr
case class Var(x: String, a: Int, n: Int) extends Expr
case class Add(n: Expr*) extends Expr
case class Mul(n: Expr*) extends Expr
def add(xs: List[Expr]): Expr = xs match {
case List() => N(0)
case List(x) => x
case _ => Add(xs.toArray: _*) // Add(WrappedArray(N(1), N(2), Var(x,2,2)))
sealed trait Expr
case class N(n: Int) extends Expr
case class Add(n: Expr*) extends Expr
case class Mul(n: Expr*) extends Expr
def str(e: Expr): String = e match {
case N(x) => x.toString
case Add() => ""
case Add(x) => str(x)
case Add(x, xs@_*) => str(x) ++ "+" ++ str(Add(xs: _*))
@7shi
7shi / deep_cpy.cpp
Last active August 29, 2015 14:21 — forked from shigemk2/deep_cpy.cpp
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct A{
char s[10000];
A() {
strcpy(s, "123");
}
@7shi
7shi / param.cpp
Last active August 29, 2015 14:20 — forked from shigemk2/param.cpp
void inc(int &x) {
++x;
}
void inc(int *x) {
++*x;
}
var fs = require("fs");
function convLE(len, v) {
var ret = "";
for (var i = 0; i < len; ++i) {
ret += String.fromCharCode(v & 0xff);
v >>= 8;
}
return ret;
}