Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8127527
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T07:33:45+00:00 2026-06-06T07:33:45+00:00

I am trying to slice a tuple, removing the last two items. I tried

  • 0

I am trying to slice a tuple, removing the last two items. I tried using the list drop/take methods but I can’t succeed to get a tuple back.

Here is the approach I tried :

scala> val myTuple = (1, 2, 4, 5, 0, 5)
myTuple: (Int, Int, Int, Int, Int, Int) = (1,2,4,5,0,5)

scala> val myList = myTuple.productIterator.toList
myList: List[Any] = List(1, 2, 4, 5, 0, 5)

scala> val mySubList = myList.dropRight(2)
mySubList: List[Any] = List(1, 2, 4, 5)

scala> val mySubTuple = ???

I saw here that list to tuple isn’t (yet?) possible in scala.

Are there other ways to get that subtuple (without dealing with myTuple._1, myTuple._2…) ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T07:33:46+00:00Added an answer on June 6, 2026 at 7:33 am

    This is the sort of thing that shapeless can do in a generic way, involving conversion into an HList.

    First – get shapeless. Then run scala with dependent method types switched on (on by default in 2.10):

    C:\Scala\sdk\scala-2.9.2\bin>scala -Ydependent-method-types
    Welcome to Scala version 2.9.2 (Java HotSpot(TM) 64-Bit Server VM, Java 1.7.0_04).
    Type in expressions to have them evaluated.
    Type :help for more information.
    

    Add shapeless to the classpath:

    scala> :cp C:\Users\cmarsha\Downloads\shapeless_2.9.2-1.2.2.jar
    Added 'C:\Users\cmarsha\Downloads\shapeless_2.9.2-1.2.2.jar'.  Your new classpath is:
    "C:\tibco\tibrv\8.2\lib\tibrvnative.jar;C:\Users\cmarsha\Downloads\shapeless_2.9.2-1.2.2.jar"
    

    Now let us play!

    scala> (1, 2.3, 'a, 'b', "c", true)
    res0: (Int, Double, Symbol, Char, java.lang.String, Boolean) = (1,2.3,'a,b,c,true)
    

    We must import shapeless

    scala> import shapeless._; import Tuples._; import Nat._
    import shapeless._
    import Tuples._
    import Nat._
    

    We turn our tuple into an HList

    scala> res0.hlisted
    res2: shapeless.::[Int,shapeless.::[Double,shapeless.::[Symbol,shapeless.::[Char,shapeless.::[java.lang.String,shapeless.::[Boolean,shapeless.HNil]]]]]] = 1 :: 2.3 :: 'a :: b :: c :: true :: HNil
    

    Then we take the first 4 (notice that _4 is a type parameter, not a method argument)

    scala> res2.take[_4]
    res4: shapeless.::[Int,shapeless.::[Double,shapeless.::[Symbol,shapeless.::[Char, shapeless.HNil]]]] = 1 :: 2.3 :: 'a :: b :: HNil
    

    Now convert back to a tuple

    scala> res4.tupled
    res5: (Int, Double, Symbol, Char) = (1,2.3,'a,b)
    

    We could shorten this:

    val (a, b, c, d) = sixtuple.hlisted.take[_4].tupled 
    //a, b, c and d would all have the correct inferred type
    

    This of course generalizes to the first M elements of an N-tuple

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been trying to slice two characters out of a string using a loop,
I am trying to slice a string and insert the components into a list
I am trying to slice the last four characters off a character array, and
I'm trying to slice a list (call this input list & it contains Java
I'm trying to deploy to a slicehost slice using this config/deploy/production.rb file. I'm clueless
I'm trying to do something which seems fairly basic, but can't seem to get
I am new to Python. Here's what I am trying to do: Slice a
I was trying to create an extension that could slice any array-like class (since
I am trying to deserialize this JSON array into my android project. [{Name:Ban,Price:1},{Name:Banana,Price:1},{Name:chicken,Price:14},{Name:pizza,Price:16},{Name:slice,Price:1}] I
I'm trying to add DatePicker VBA control in an PowerPoint 2007 slide. But I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.