Skip to content

Instantly share code, notes, and snippets.

Attoparsec Cheatsheet (vs. Regex)

Preconditions

  • OverloadedString extension is applied.
  • The following modules are imported.
    • Control.Applicative
    • Data.Attoparsec.Text
    • Data.Char
import Data.List (foldl', intercalate)
import Test.HUnit (Test(..), runTestTT, (~=?))
process :: String -> String
process = intercalate "," . map show . take 10 . foldl' transf [1..]
isSquare n = let x = sqrt (fromIntegral n) in x - fromIntegral (floor x) == 0
--isCube n = let x = (fromIntegral n) ** (1 / 3) in x - fromIntegral (floor x) == 0
isCube n = round (fromIntegral n ** (1 / 3)) ^ 3 == n
Inductive term : Type :=
| Ttrue : term
| Tfalse : term
| Tifthenelse : term -> term -> term -> term.
Inductive evaluate : term -> term -> Prop :=
| Eiftrue : forall t2 t3, evaluate (Tifthenelse Ttrue t2 t3) t2
| Eiffalse : forall t2 t3, evaluate (Tifthenelse Tfalse t2 t3) t3
| Eif : forall t1 t1' t2 t3, evaluate t1 t1' -> evaluate (Tifthenelse t1 t2 t3) (Tifthenelse t1' t2 t3).
Inductive term : Type :=
| Ttrue : term
| Tfalse : term
| Tifthenelse : term -> term -> term -> term.
Inductive evaluate : term -> term -> Prop :=
| Eiftrue : forall t2 t3, evaluate (Tifthenelse Ttrue t2 t3) t2
| Eiffalse : forall t2 t3, evaluate (Tifthenelse Tfalse t2 t3) t3
| Eif : forall t1 t1' t2 t3, evaluate t1 t1' -> evaluate (Tifthenelse t1 t2 t3) (Tifthenelse t1' t2 t3).
Inductive term : Type :=
| Ttrue : term
| Tfalse : term
| Tifthenelse : term -> term -> term -> term.
Inductive evaluate : term -> term -> Prop :=
| Eiftrue : forall t2 t3, evaluate (Tifthenelse Ttrue t2 t3) t2
| Eiffalse : forall t2 t3, evaluate (Tifthenelse Tfalse t2 t3) t3
| Eif : forall t1 t1' t2 t3, evaluate t1 t1' -> evaluate (Tifthenelse t1 t2 t3) (Tifthenelse t1' t2 t3).
@takeouchida
takeouchida / Main.scala
Created January 22, 2015 14:08
spray-client proxy example based on a code in spray repository
package clientwithproxy
import akka.util.Timeout
import spray.can.Http.ClientConnectionType
import scala.util.{Success, Failure}
import scala.concurrent.duration._
import akka.actor.ActorSystem
import akka.pattern.ask
import akka.event.Logging
@takeouchida
takeouchida / log.py
Created June 25, 2015 02:22
A way to log
import os
import traceback
# Write a message to /var/log/hoge.log
os.system("echo `date '+%Y/%m/%d %H:%M:%S.%N'` 'Some message' >> /var/log/hoge.log")
# Write a traceback to /var/log/hoge.log
os.system("echo `date '+%Y/%m/%d %H:%M:%S.%N'` '" + traceback.format_exc() + "' >> /var/log/hoge.log")
@takeouchida
takeouchida / Ipv4.java
Created October 3, 2015 03:51
An annotation class for validating IPv4 addresses with Hibernate Validator.
package hibernatevalidator.constraints;
import javax.validation.Constraint;
import javax.validation.Payload;
import javax.validation.ReportAsSingleViolation;
import javax.validation.constraints.Pattern;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
@takeouchida
takeouchida / Macaddr.java
Created October 3, 2015 04:05
An annotation class for validating mac addresses with Hibernate Validator.
package hibernatevalidator.constraints;
import javax.validation.Constraint;
import javax.validation.Payload;
import javax.validation.ReportAsSingleViolation;
import javax.validation.constraints.Pattern;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
@takeouchida
takeouchida / PostgresInetBinding.java
Created October 4, 2015 03:59
A binding class between Postgres inet type and Java string with JOOQ.
// See this for details.
// http://www.jooq.org/doc/latest/manual/code-generation/custom-data-type-bindings/
package postgrestypes.bindings;
import org.jooq.*;
import org.jooq.impl.DSL;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;