shapeless가 매핑 및 중간 HList
표현을 통해 튜플을 통해 폴딩을 지원 UPDATE,
샘플 REPL 세션,
scala> import shapeless._ ; import Tuples._
import shapeless._
import Tuples._
scala> object double extends (Int -> Int) (_*2)
defined module double
scala> (3, 4).hlisted.map(double).tupled
res0: (Int, Int) = (6,8)
012 튜플의 요소는 타입 별 사례와 다형성 기능을 매핑 할 수 있습니다 다른 종류의 수 있습니다 3,516,
, 튜플 이상 볼품 2.0.0-M1 매핑으로
scala> object frob extends Poly1 {
| implicit def caseInt = at[Int](_*2)
| implicit def caseString = at[String]("!"+_+"!")
| implicit def caseBoolean = at[Boolean](!_)
| }
defined module frob
scala> (23, "foo", false, "bar", 13).hlisted.map(frob).tupled
res1: (Int, String, Boolean, String, Int) = (46,!foo!,true,!bar!,26)
업데이트
직접 지원됩니다. 위의 예제는 이제 다음과 같이 보입니다.
scala> import shapeless._, poly._, syntax.std.tuple._
import shapeless._
import poly._
import syntax.std.tuple._
scala> object double extends (Int -> Int) (_*2)
defined module double
scala> (3, 4) map double
res0: (Int, Int) = (6,8)
scala> object frob extends Poly1 {
| implicit def caseInt = at[Int](_*2)
| implicit def caseString = at[String]("!"+_+"!")
| implicit def caseBoolean = at[Boolean](!_)
| }
defined module frob
scala> (23, "foo", false, "bar", 13) map frob
res1: (Int, String, Boolean, String, Int) = (46,!foo!,true,!bar!,26)
실제로 컬렉션을 사용해야하는 튜플을 사용하고있는 것처럼 보입니다. 실제 컬렉션 클래스를 대신 사용해보십시오 - 튜플을 일종의 컬렉션으로 사용해서는 안됩니다. – Jesper
@Jesper : 나는 동의하지 않는다 : 나는 단순히 같은 작업을 DRY'ly 적용하고 크기가 정적으로 알려진 항목의 모음에 합리적으로 적용하기를 원할 수도있다. –