Skip to content

Instantly share code, notes, and snippets.

@satabin
satabin / Parser.scala
Created November 7, 2012 12:40
Simple Monadic Parser Combinator à la Parsec
/** Primitive parser combinators.
* Input is a stream of tokens of any kind you wish.
*/
trait Prim[Token] {
/** A parser is a function that takes a token stream as parameter and returns some parsed result,
* the number of consumed tokens and the remaining input.
*/
trait Parser[+T] extends (Stream[Token] => (Result[T], Int, Stream[Token])) {
package main
func fact(i int) int {
var aux func(int, int) int
aux = func(k int, acc int) int {
if(k == 0) {
return acc
} else {
return aux(k - 1, k * acc)
}
/** Mixin this trait if you want the following strategy for matcher:
* all matching branches are executed in the order they appear in the pattern matching block */
trait AllMatcher {
sealed trait Maybe[+T] {
def map[U](f: T => U): Maybe[U]
def flatMap[U](f: T => Maybe[U]): Maybe[U]
def orElse[U >: T](u: =>Maybe[U]): Maybe[U]
def getOrElse[U >: T](u: =>U): U
}
import scala.annotation.tailrec
class Patience[T] {
/** An occurrence of a value associated to its index */
type Occurrence = (T, Int)
/** Returns occurrences that appear only once in the list, associated with their index */
private def uniques(l: List[T]): List[Occurrence] = {
@tailrec
scala> import gnieh.sohva.conflict._
import gnieh.sohva.conflict._
scala> import net.liftweb.json.{parse, render, pretty}
import net.liftweb.json.{parse, render, pretty}
scala> val json1 = parse("""{
| "lbl1": true,
| "lbl2": [1, 2, 3],
| "lbl3": {"lbl": 23}
\documentclass[a4paper]{article}
\begin{document}
\input{truie} % doesn't work
\include{truie} % works
\end{document}
@satabin
satabin / Monadic.scala
Created April 10, 2013 17:03
better monadic parser combinator with lazy evaluation for better performance
package gnieh.tex
import scala.language.higherKinds
/** A generic monadic interface. By implementing this trait, one can use
* this data together in for-comprehensions
*
* @author Lucas Satabin
*/
trait Monadic[+T, M[+T] <: Monadic[T, M]] {
package gnieh.test
import com.typesafe.config._
import java.io.File
import java.net.{
URLClassLoader,
URL
}
package dispatcher
import akka.actor._
import scala.collection.mutable.{
Map,
Set
}
final case class Join(username: String, resourceid: String)
/*
* This file is part of the \BlueLaTeX project.
*
* 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