Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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 / 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
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).
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

Attoparsec Cheatsheet (vs. Regex)

Preconditions

  • OverloadedString extension is applied.
  • The following modules are imported.
    • Control.Applicative
    • Data.Attoparsec.Text
    • Data.Char
@takeouchida
takeouchida / reduce.v
Last active December 29, 2015 16:49
Monoid type class and its instances.
Require Import List Arith ZArith Program.
Class Monoid {A} (dot : A -> A -> A) (zero : A) := {
monoid_1st_law : forall a, dot a zero = a;
monoid_2nd_law : forall a, dot zero a = a;
monoid_3rd_law : forall a b c, dot (dot a b) c = dot a (dot b c)
}.
Instance nat_plus_Monoid : Monoid plus 0.
split; auto with arith.