Skip to content

Instantly share code, notes, and snippets.

View hiramekun's full-sized avatar
🎯
Focusing

Takaaki Hirano hiramekun

🎯
Focusing
View GitHub Profile
@hiramekun
hiramekun / CollectFirstSome.scala
Created March 11, 2023 03:36
CollectFirstSome
private def collectFirstSome[A, B](seq: Seq[A])(fn: A => Option[B]): Option[B] = {
seq.collectFirst(Function.unlift(fn))
}
@hiramekun
hiramekun / MonocleSample.scala
Created April 24, 2022 10:04
Lens Pattern with Monocle
import monocle.Monocle.toAppliedFocusOps
case class Country(name: String, code: String)
case class City(name: String, country: Country)
case class Address(number: Int, street: String, city: City)
case class Company(name: String, address: Address)
@hiramekun
hiramekun / MyOption.scala
Last active April 20, 2022 02:14
Option monad example using cats.Monad.
import cats.Monad
import scala.annotation.tailrec
object MyOption {
implicit def optionMonad: Monad[MyOption] =
new Monad[MyOption] {
override def pure[A](x: A): MyOption[A] = MySome(x)
override def flatMap[A, B](fa: MyOption[A])(f: A => MyOption[B]): MyOption[B] = fa match {
case MySome(value) => f(value)
@hiramekun
hiramekun / MyOrderingNum.scala
Created April 12, 2021 02:39
MyOrderingNum
case class MyOrderedNum(value: Int) extends Ordered[MyOrderedNum] {
override def compare(that: MyOrderedNum): Int = {
if (this.value < that.value) -1
else if (that.value < this.value) 1
else 0
}
}
case class MyOrderingNum(value: Int)(implicit ord: Ordering[MyOrderingNum]) {
def >(that: MyOrderingNum): Boolean = ord.compare(this, that) > 0
import akka.actor.{Actor, ActorSystem, Props}
object HelloWorld extends App {
val system = ActorSystem("system")
val ref = system.actorOf(Props[MyActor])
ref ! "HelloWorld"
}
class MyActor extends Actor {
override def receive: Receive = {
case msg => println(msg)
@hiramekun
hiramekun / parameter.scala
Created July 10, 2020 09:28
反変、共変周りの練習
class Publication(val title: String)
class Book(title: String) extends Publication(title)
object Library {
val books: Set[Book] = Set(new Book("Programming in Scala"), new Book("Walden"))
def printBookList(info: Book => AnyRef) = {
for (book <- books) println(info(book))
@hiramekun
hiramekun / draw_contour.py
Created October 28, 2018 14:28
等高線を描くサンプル
def draw_contour(target):
n = 100
x = np.linspace(-5, 5, n)
y = np.linspace(-5, 5, n)
X, Y = np.meshgrid(x, y)
Z = target([X, Y])
plt.gca().set_aspect('equal')
plt.contour(X, Y, Z, levels=np.linspace(0, 200, 15))
@hiramekun
hiramekun / malloc_sample.c
Created October 16, 2018 00:12
mallocの動作確認
#include <stdio.h>
#include <stdlib.h>
main() {
char *a = malloc(1000);
char *b = malloc(1000);
char *c = malloc(1000);
char *d = malloc(1000);
printf("a:%lu\n", a);
printf("b:%lu\n", b);
@hiramekun
hiramekun / plt.txt
Created October 14, 2018 16:49
matplotlib.pyplotをjupyterで使うときのテンプレート
import matplotlib.pyplot as plt
%matplotlib inline
plt.rcParams['figure.figsize'] = (15.0, 15.0)