Skip to content

Instantly share code, notes, and snippets.

Avatar

Yuki Toyoda yuk1ty

  • Tokyo, Japan
View GitHub Profile
@yuk1ty
yuk1ty / validated_str.rs
Last active Jul 1, 2021
Create string type validated
View validated_str.rs
use std::{marker::PhantomData, str::FromStr};
use super::error::ValidationError;
pub trait ValidationStrategy {
fn validate(target: &str) -> Result<String, ValidationError>;
}
pub struct NonEmptyString<V>
where
@yuk1ty
yuk1ty / cat.rs
Created Mar 17, 2021
Rustハンズオンで使用したコード
View cat.rs
// 実行時引数を受け取って cat するプログラム
fn run(path: String) {
match std::fs::read_to_string(path) {
Ok(content) => print!("{}", content),
Err(why) => println!("{}", why),
}
}
fn main() {
let mut args = std::env::args();
match args.nth(1) {
@yuk1ty
yuk1ty / Saga.scala
Created Jul 1, 2020
Saga implementation for com.twitter.util.Future
View Saga.scala
package jp.airtrack.bid.util.saga
import com.twitter.util.Future
import jp.airtrack.bid.util.saga.Saga.SagaErr
sealed abstract class Saga[A] {
def map[B](f: A => B): Saga[B] = flatMap(a => Saga.Succeeded(f(a)))
def flatMap[B](f: A => Saga[B]): Saga[B] = Saga.FlatMap(this, (a: A) => f(a))
@yuk1ty
yuk1ty / IO.scala
Created Dec 28, 2019
scratch-io-monad
View IO.scala
sealed trait IO[+A] {
import IO._
def map[B](f: A => B): IO[B] = this match {
case Map(original, g, index) if index != 128 =>
Map(original, g.andThen(f), index + 1)
case _ => Map(this, f, 0)
}
View future_exercise.rs
use std::future::Future;
use std::ops::{Deref, DerefMut};
use std::pin::Pin;
use std::sync::mpsc::{channel, Receiver};
use std::sync::{Arc, Condvar, Mutex};
use std::task::{Context, Poll, RawWaker, RawWakerVTable, Waker};
use std::thread;
pub struct ReturnFuture<T>(Option<T>);
@yuk1ty
yuk1ty / ChainedHashTable.scala
Created Dec 8, 2018
アルゴリズムの練習 in Scala
View ChainedHashTable.scala
import scala.collection.mutable
import scala.util.Random
case class ChainedHashTable[A](private var t: Array[mutable.ListBuffer[A]],
private var d: Int,
private var n: Int = 0,
private var z: Int)
extends USet[A] {
private[this] lazy val w: Int = 32
@yuk1ty
yuk1ty / main.rs
Created Jul 27, 2018
simple interpreter with simple virtual machine
View main.rs
use std::fmt;
use std::cell::RefCell;
pub struct Machine {
pub expression: RefCell<Token>,
}
impl Machine {
pub fn new(expression: Token) -> Self {
Machine {
View HttpServerImpl.java
private void doRead(SocketChannel socketChannel) throws NioHttpServerException {
try {
ByteBuffer buf = ByteBuffer.allocate(1024);
socketChannel.read(buf);
buf.flip();
Option<Request> maybeRequest = requestHandler.apply(buf);
buf.clear();
Option<Either<Exception, Response>> maybeResponse =
View ServerSample.java
public void start() throws NioHttpServerException {
InputStream inputStream = inputStream();
requestHandler
.apply(inputStream)
.toStream()
.map(
v -> {
switch (v.status()) {
case OK:
View ThrowableSupplier.java
@FunctionalInterface
public interface ThrowableSupplier<T> {
T get() throws Exception;
}