복붙노트

[SCALA] 스칼라 튜플 풀고

SCALA

스칼라 튜플 풀고

이 질문은 다른 방법으로 몇 번을왔다 알고있다. 하지만 여전히 나에게 분명하지 않다. 다음을 달성 할 수있는 방법이 있나요.

def foo(a:Int, b:Int) = {}

foo(a,b) //right way to invoke foo

foo(getParams) // is there a way to get this working without explicitly unpacking the tuple??

def getParams = {
   //Some calculations
   (a,b)  //where a & b are Int
}

해결법

  1. ==============================

    1.그것은 두 단계 절차이다. 다음에 tupled 호출 함수에 먼저 턴 foo는, 그것이 튜플의 기능을 확인합니다.

    그것은 두 단계 절차이다. 다음에 tupled 호출 함수에 먼저 턴 foo는, 그것이 튜플의 기능을 확인합니다.

    (foo _).tupled(getParams)
    
  2. ==============================

    2.@ 데이브 - 그리피스는 죽은입니다.

    @ 데이브 - 그리피스는 죽은입니다.

    또한 호출 할 수 있습니다 :

    Function.tupled(foo _)
    

    당신이에 영토 "방법 내가 요구보다 더 많은 정보를"방황하고 싶다면, 또한 태닝 부분적으로 적용 기능에 내장 방법 (및 기능)에있다. 몇 가지 입력 / 출력 예 :

    scala> def foo(x: Int, y: Double) = x * y
    foo: (x: Int,y: Double)Double
    
    scala> foo _
    res0: (Int, Double) => Double = <function2>
    
    scala> foo _ tupled
    res1: ((Int, Double)) => Double = <function1>
    
    scala> foo _ curried
    res2: (Int) => (Double) => Double = <function1>
    
    scala> Function.tupled(foo _)
    res3: ((Int, Double)) => Double = <function1>
    
    // Function.curried is deprecated
    scala> Function.curried(foo _)
    warning: there were deprecation warnings; re-run with -deprecation for details
    res6: (Int) => (Double) => Double = <function1>
    

    상기 카레 버전은 여러 인자리스트를 호출 :

    scala> val c = foo _ curried
    c: (Int) => (Double) => Double = <function1>
    
    scala> c(5)
    res13: (Double) => Double = <function1>
    
    scala> c(5)(10)
    res14: Double = 50.0
    

    마지막으로, 당신은 uncurry 수도 있습니다 / 필요한 경우 untuple. 기능이에 대한 내장 명령이 있습니다 :

    scala> val f = foo _ tupled
    f: ((Int, Double)) => Double = <function1>
    
    scala> val c = foo _ curried
    c: (Int) => (Double) => Double = <function1>
    
    scala> Function.uncurried(c)
    res9: (Int, Double) => Double = <function2>
    
    scala> Function.untupled(f)
    res12: (Int, Double) => Double = <function2>
    

  3. ==============================

    3.Function.tupled (foo는 _ ()의 getParams) 또는 데이브에 의해 제안 하나. 편집하다:

    Function.tupled (foo는 _ ()의 getParams) 또는 데이브에 의해 제안 하나. 편집하다:

    귀하의 코멘트에 응답하려면 :

    이 경우,이 트릭이 작동하지 않습니다.

    당신은 당신의 클래스의 동반자 객체의 팩토리 메소드를 작성하고는 상기 기술 중 하나를 사용하는 방법을 적용의 다음 tupled 버전을 얻을 수 있습니다.

    scala> class Person(firstName: String, lastName: String) {
         |   override def toString = firstName + " " + lastName
         | }
    defined class Person
    
    scala> object Person {
         |   def apply(firstName: String, lastName: String) = new Person(firstName, lastName)
         | }
    defined module Person
    
    scala> (Person.apply _).tupled(("Rahul", "G"))
    res17: Person = Rahul G
    

    케이스 클래스와 함께 당신은 가진 동반자 객체가 무료로 방법을 적용 받고, 따라서이 기술은 경우 클래스를 사용하는 것이 더 편리합니다.

    scala> case class Person(firstName: String, lastName: String)
    defined class Person
    
    scala> Person.tupled(("Rahul", "G"))
    res18: Person = Person(Rahul,G)
    

    내가 그 코드 중복 그러나 슬프게도 많은 알고 ... 우리는 (아직) 매크로가 없습니다! ;)

  4. ==============================

    4.나는 가까이 당신이 무엇을 요구했다 다른 몇 가지 답변에 감사하지만 개종자는 분할 매개 변수에 매개 변수를 튜플 다른 기능을 추가 할 수있는 현재 프로젝트 쉽게 발견 :

    나는 가까이 당신이 무엇을 요구했다 다른 몇 가지 답변에 감사하지만 개종자는 분할 매개 변수에 매개 변수를 튜플 다른 기능을 추가 할 수있는 현재 프로젝트 쉽게 발견 :

    def originalFunc(a: A, b: B): C = ...
    def wrapperFunc(ab: (A, B)): C = (originalFunc _).tupled(ab)
    
  5. ==============================

    5.지금, 당신은 foo는 구현하고 너무 같은 Tuple2 클래스의 PARAM을 만들 수 있습니다.

    지금, 당신은 foo는 구현하고 너무 같은 Tuple2 클래스의 PARAM을 만들 수 있습니다.

    def foo(t: Tuple2[Int, Int]) = {
      println("Hello " + t._1 + t._2)
      "Makes no sense but ok!"
    }
    
    def getParams = {
      //Some calculations
      val a = 1;
      val b = 2;
      (a, b) //where a & b are Int
    }
    
    // So you can do this!
    foo(getParams)
    // With that said, you can also do this!
    foo(1, 3)
    
  6. from https://stackoverflow.com/questions/3568002/scala-tuple-unpacking by cc-by-sa and MIT license