Skip to content

Instantly share code, notes, and snippets.

View kamiyaowl's full-sized avatar
🤔
:thinking_face:

Kamiya kamiyaowl

🤔
:thinking_face:
View GitHub Profile
def fizz (x:Int) : (Int, Boolean) = x match { case _ if x % 3 == 0 => (x,true) ; case _ => (x,false) }
def buzz (x:((Int, Boolean))) : (Int, Boolean, Boolean) = x._1 match { case n if n % 5 == 0 => (x._1, x._2,true) ; case _ => (x._1,x._2,false) }
def fizzbuzz(x:((Int,Boolean,Boolean))) : String = x match {
case (_, true, true) => "FizzBuzz"
case (_, true, false) => "Fizz"
case (_, false, true) => "Buzz"
case (n,_,_) => n.toString
}
implicit class FizzBuzz[A](self:A) {
def fizz(x:Any) = self
/**
* Created by kamiya on 2015/02/16.
*/
package kamiya.parse.brainfuck
import scala.util.parsing.combinator.RegexParsers
class BrainfuckProcessor {
private var pt : Int = 0
private var buf : Array[Char] = Array.fill[Char](3000)(0)
def Y[A,B]( f:((A => B), A ) => B, x:A ):B = f( (y:A) => Y(f,y),x)
((size:(Int,Int)) => Y(
(f:Map[(Int,Int),Boolean] => Stream[Map[(Int,Int),Boolean]], seed:Map[(Int,Int),Boolean]) =>
((curr : Map[(Int,Int),Boolean]) => Stream.cons(curr,f(curr))) {
( for (x <- 0 to size._1; y <- 0 to size._2) yield List(
seed.get(x - 1, y - 1), seed.get(x, y - 1), seed.get(x + 1, y - 1), seed.get(x - 1, y), seed.get(x + 1, y), seed.get(x - 1, y + 1), seed.get(x, y + 1), seed.get(x + 1, y + 1))
count { case Some(z) => z; case None => false }
match { case z if !seed(x, y) && z == 3 => (x, y) -> true ; case z if seed(x, y) && (z == 2 || z == 3) => (x, y) -> true ; case _ => (x, y) -> false }
).toMap },
((s:(Int,Int)) => { for(x <- 0 to size._1 ; y <- 0 to size._2) yield (x,y) -> util.Random.nextBoolean }.toMap)(size)
@kamiyaowl
kamiyaowl / Main.scala
Last active August 29, 2015 14:15
簡易数式パーサ
/**
* Created by kamiya on 2015/02/11.
*/
import util.parsing.combinator._
object Main {
class Expr
class Literal extends Expr
case class ConstNumber[+A](num: A) extends Literal
@kamiyaowl
kamiyaowl / Program.cs
Last active August 29, 2015 14:14
LinqExtension
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication3 {
class Program {
static void Main(string[] args) {
@kamiyaowl
kamiyaowl / Program.cs
Last active August 29, 2015 14:14
fizzbuzz
using System;
using System.Linq;
namespace ConsoleApplication1 {
class Program {
static void Main(string[] args) {
var f = new[] { 0xff, 0x80, 0x80, 0xf0, 0x80, 0x80, 0x80, 0x80 };
var i = new[] { 0xe0, '@', '@', 0x40, '@', '@', '@', 0xe0 };
var z = new[] { 0xff, 0x02, 0x04, 0x08, 0x10, ' ', '@', 0xff };
@kamiyaowl
kamiyaowl / Program.cs
Last active August 29, 2015 14:14
linqとか比較しながらselectとか
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication3 {
class Program {
static void Main(string[] args) {
var src = LinqExtension.RandomSource(10).Take(10).ToArray();
@kamiyaowl
kamiyaowl / fizzbuzz.il
Created January 16, 2015 14:52
FizzBuzz in IL
.assembly extern mscorlib {}
.assembly fizzbuzz {}
.method static void Main() cil managed {
.entrypoint
.maxstack 2
.locals init (int32 i, int32 n)
ldc.i4 1
ldstr "fizzbuzz max>"
@kamiyaowl
kamiyaowl / vfd_emit.ino
Last active March 5, 2017 14:40
VFD(IV-18)を光らせる
const uint8_t sw_out = 9;
uint16_t target_value = 1023;
const uint8_t segments_pin[] = {0, 1, 2, 3, 4, 5, 6};
const uint8_t orders_pin[] = {A4, A3, A2, A1, 13, 12, 11, 10};
const uint8_t segments_count = 7;
const uint8_t orders_count = 8;
const uint8_t segment_data[] = {
0b11111100,//0
0b01100000,//1
@kamiyaowl
kamiyaowl / monte.scala
Created January 13, 2015 14:46
モンテカルロ法
def src : Stream[(Double,Double)] = (util.Random.nextDouble, util.Random.nextDouble) #:: src
def monte(n:Int) : Double = {src take n map(x => x._1 * x._1 + x._2 * x._2) count (_ < 1.0) } / n.toDouble * 4
println(monte(1000))