Skip to content

Instantly share code, notes, and snippets.

View debop's full-sized avatar

Sunghyouk Bae debop

View GitHub Profile
@debop
debop / package.scala
Created January 18, 2016 06:32
Scala Implicit methods for Java 8 Functions
package kesti4j
import java.util.function._
/**
* @author sunghyouk.bae@gmail.com
*/
package object core {
object Implicits {
@debop
debop / TestTool.java
Created January 10, 2013 05:27
Run runnable code block in parallel mode by java
private static ExecutorService newExecutorService() {
return Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
}
@SafeVarargs
public static void runTasks(int count, Runnable... runnables) {
ExecutorService executor = newExecutorService();
try {
@Configuration
@EnableCaching
@ComponentScan(basePackageClasses = UserRepository.class)
// @PropertySource("classpath:redis.properties")
public class RedisCacheConfiguration {
@Autowired
Environment env;
@Bean
@debop
debop / AvroSerializer.kt
Created February 27, 2017 14:31
AvroSerializer
open class AvroSerializer @JvmOverloads constructor(val codecFactory: CodecFactory = CodecFactory.snappyCodec()) {
/**
* Avro instance 를 직렬화하여 byte array 로 변환합니다.
*/
fun <T : SpecificRecordBase> writeAvroObject(graph: T?): ByteArray {
return graph?.let {
val sdw = SpecificDatumWriter<T>(graph.schema)
val dfw = DataFileWriter(sdw).setCodec(codecFactory)
@debop
debop / package.scala
Created February 5, 2017 09:34
Port apply, let method in Kotlin Standard library to Scala
/** 모든 수형에 대해 builder 패턴을 제공하기 위해 사용 : Kotlin 의 apply method 와 같다 */
implicit class AnyRefExtension[T <: AnyRef](underlying: T) {
def build(builder: T => T): T = builder(underlying)
def let(procedure: T => Unit): Unit = procedure(underlying)
}
/** 모든 Option 에 대해 Kotlin 의 let 과 같은 기능을 제공한다 */
implicit class OptionExteions[T](underlying: Option[T]) {
def let(procedure: T => Unit): Unit = underlying foreach { procedure }
}
@debop
debop / Logger.scala
Created January 9, 2013 03:05
Scala 용 Slf4j Logger
package kr.kth.commons.slf4j
import org.slf4j.{Logger => Slf4jLogger, Marker}
import annotation.varargs
/**
* Scala를 위한 Logger 입니다.
* User: sunghyouk.bae@gmail.com
* Date: 13. 1. 6.
*/
@debop
debop / Products.kt
Created September 4, 2016 16:18
Product in Kotlin ported from Scala
interface Product {
val productArity: Int
fun elements(index: Int): Any?
fun toList(): List<*> = productIterator().toList()
fun productIterator(): Iterator<*> = object : Iterator<Any?> {
var currIndex = 0
override fun hasNext(): Boolean = currIndex < productArity
override fun next(): Any? = elements(currIndex++)
@debop
debop / Tuples.kt
Created September 4, 2016 16:17
Tuples in Kotlin ported from Scala
data class Tuple1<out T1 : Any>(override val first: T1) : Product1<T1>, Serializable {
override fun toString(): String {
return "($first)"
}
}
data class Tuple2<out T1 : Any, out T2 : Any>(
override val first: T1,
override val second: T2) : Product2<T1, T2>, Serializable {
@debop
debop / ParallelIterates.kt
Created July 26, 2016 09:09
Kotlin 과 EclipseCollection 으로 구현한 병렬 처리 루틴
/*
* Copyright (c) 2016. Sunghyouk Bae <sunghyouk.bae@gmail.com>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
@debop
debop / GsCollectionsFunSuite.scala
Created January 17, 2016 15:26
using gs-collections with scala lambda expression.
class GsCollectionsFunSuite extends AbstractCoreFunSuite {
test("FastList collection") {
val fastlist = FastList.newListWith[Int](1, 2, 3, 4, 5)
val collected: FastList[Int] = fastlist.collect(new Function[Int, Int] {
override def valueOf(obj: Int): Int = obj * obj
})
collected should contain allOf(1, 4, 9, 16, 25)