Merging sequential lists with non-matching indices

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Merging sequential lists with non-matching indices

Jason Nerothin
I'd like to 'zip'  a few datatypes: 

IndexedSeq[(Long,Foo)] and an IndexedSeq[(Long,Bar)]

case class Foo()
case class Bar()

val f = Foo()
val g = Foo()
val h = Foo()

val s = Bar()
val t = Bar()

val a = immutable.Vector((1,f),(2,g)(4,h),(7,f))
val b = immutable.Vector((1,s),(3,t),(9,t))

The result I want is: 

Seq[(1,f,s),(2,g,t),(4,h,t),(7,h,t)]

As you can see, in the case where there is a "miss in a", I want it to move up the next Bar of b, no matter how far away from a's index it happens to be. (A is dense, b is sparse. But their indices are relatively close to one another.) 


--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Merging sequential lists with non-matching indices

Vlad Patryshev
a zipWith (b map (_._2)) should work, no?

Thanks,
-Vlad

On Sat, Jan 7, 2017 at 8:13 AM, Jason Nerothin <[hidden email]> wrote:
I'd like to 'zip'  a few datatypes: 

IndexedSeq[(Long,Foo)] and an IndexedSeq[(Long,Bar)]

case class Foo()
case class Bar()

val f = Foo()
val g = Foo()
val h = Foo()

val s = Bar()
val t = Bar()

val a = immutable.Vector((1,f),(2,g)(4,h),(7,f))
val b = immutable.Vector((1,s),(3,t),(9,t))

The result I want is: 

Seq[(1,f,s),(2,g,t),(4,h,t),(7,h,t)]

As you can see, in the case where there is a "miss in a", I want it to move up the next Bar of b, no matter how far away from a's index it happens to be. (A is dense, b is sparse. But their indices are relatively close to one another.) 


--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Merging sequential lists with non-matching indices

Jason Nerothin
Not quite, I don't think.

If there are 10,000 elements in a and 1,000 elements in b, I'll end up with a list 1,000 long.

Each Foo needs the nearest Bar, where index(a) <= index(b). In the edge case where there is no index(b) <= index(a) - i.e. tail of a - they can be dropped.



On Saturday, January 7, 2017 at 10:18:45 AM UTC-6, Vlad Patryshev wrote:
a zipWith (b map (_._2)) should work, no?

Thanks,
-Vlad

On Sat, Jan 7, 2017 at 8:13 AM, Jason Nerothin <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="Pm4D5ewPDwAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">jasonn...@...> wrote:
I'd like to 'zip'  a few datatypes: 

IndexedSeq[(Long,Foo)] and an IndexedSeq[(Long,Bar)]

case class Foo()
case class Bar()

val f = Foo()
val g = Foo()
val h = Foo()

val s = Bar()
val t = Bar()

val a = immutable.Vector((1,f),(2,g)(4,h),(7,f))
val b = immutable.Vector((1,s),(3,t),(9,t))

The result I want is: 

Seq[(1,f,s),(2,g,t),(4,h,t),(7,h,t)]

As you can see, in the case where there is a "miss in a", I want it to move up the next Bar of b, no matter how far away from a's index it happens to be. (A is dense, b is sparse. But their indices are relatively close to one another.) 


--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="Pm4D5ewPDwAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">scala-user+...@googlegroups.com.
For more options, visit <a href="https://groups.google.com/d/optout" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;">https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Merging sequential lists with non-matching indices

Jason Nerothin
In reply to this post by Jason Nerothin


On Saturday, January 7, 2017 at 10:13:07 AM UTC-6, Jason Nerothin wrote:
I'd like to 'zip'  a few datatypes: 

IndexedSeq[(Long,Foo)] and an IndexedSeq[(Long,Bar)]

case class Foo()
case class Bar()

val f = Foo()
val g = Foo()
val h = Foo()

val s = Bar()
val t = Bar()

val a = immutable.Vector((1,f),(2,g)(4,h),(7,f))
val b = immutable.Vector((1,s),(3,t),(9,t))

The result I want is: 

Seq[(1,f,s),(2,g,t),(4,h,t),(7,h,t)]

As you can see, in the case where there is a "miss in a", I want it to move up the next Bar of b, no matter how far away from a's index it happens to be. (A is dense, b is sparse. But their indices are relatively close to one another.) 


--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Merging sequential lists with non-matching indices

Lanny Ripple
In reply to this post by Jason Nerothin
You'll probably want to rebuild the as and manage the bs explicitly.  Something like (off the top of my head so mostly for the idea)

val as = ???
val bs
= ???
val newAs
= List.empty[(Int,String,String)]
var workingBs = bs

as.foreach{case(n,v) =>
 val tbs
= workingBs.dropWhile(_._1 < n)
 
if (tbs.nonEmpty) {
   newAs
+= (n, v,tbs.head._2)
   workingBs
= tbs.tail
 
}else{
   
// requirements don't specify if this can happen
 
}
}



On Saturday, January 7, 2017 at 10:13:07 AM UTC-6, Jason Nerothin wrote:
I'd like to 'zip'  a few datatypes: 

IndexedSeq[(Long,Foo)] and an IndexedSeq[(Long,Bar)]

case class Foo()
case class Bar()

val f = Foo()
val g = Foo()
val h = Foo()

val s = Bar()
val t = Bar()

val a = immutable.Vector((1,f),(2,g)(4,h),(7,f))
val b = immutable.Vector((1,s),(3,t),(9,t))

The result I want is: 

Seq[(1,f,s),(2,g,t),(4,h,t),(7,h,t)]

As you can see, in the case where there is a "miss in a", I want it to move up the next Bar of b, no matter how far away from a's index it happens to be. (A is dense, b is sparse. But their indices are relatively close to one another.) 


--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Merging sequential lists with non-matching indices

Jason Nerothin
Forgot to mention that as is a Stream, and therefore I have an iterator:

var currA = aItr.next
val asAndBs: IndexedSeq[(Long, A, B)] = asAndCs.map { ac =>
val time = ac._1
if (time < currA.timestampMs && aItr.hasNext) {
currA = aItr.next
(time, ac._1, ac._2, currA)
}
}


On Monday, January 9, 2017 at 3:17:05 PM UTC-6, Lanny Ripple wrote:
You'll probably want to rebuild the as and manage the bs explicitly.  Something like (off the top of my head so mostly for the idea)

val as = ???
val bs
= ???
val newAs
= List.empty[(Int,String,String)]
var workingBs = bs

as.foreach{case(n,v) =>
 val tbs
= workingBs.dropWhile(_._1 < n)
 
if (tbs.nonEmpty) {
   newAs
+= (n, v,tbs.head._2)
   workingBs
= tbs.tail
 
}else{
   
// requirements don't specify if this can happen
 
}
}



On Saturday, January 7, 2017 at 10:13:07 AM UTC-6, Jason Nerothin wrote:
I'd like to 'zip'  a few datatypes: 

IndexedSeq[(Long,Foo)] and an IndexedSeq[(Long,Bar)]

case class Foo()
case class Bar()

val f = Foo()
val g = Foo()
val h = Foo()

val s = Bar()
val t = Bar()

val a = immutable.Vector((1,f),(2,g)(4,h),(7,f))
val b = immutable.Vector((1,s),(3,t),(9,t))

The result I want is: 

Seq[(1,f,s),(2,g,t),(4,h,t),(7,h,t)]

As you can see, in the case where there is a "miss in a", I want it to move up the next Bar of b, no matter how far away from a's index it happens to be. (A is dense, b is sparse. But their indices are relatively close to one another.) 


--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Merging sequential lists with non-matching indices

Jason Nerothin
In reply to this post by Jason Nerothin
Forgot to mention that it's a Stream and has an Iterator... Here's what I settled on:

var currA = aItr.next
val timesAndAsAndBsAndCs: IndexedSeq[(Long, Trade, Double, TBill)] = timesAndAsAndBs.map { tab =>
val time = tar._1
if (time < currA.timestampMs && aItr.hasNext) {
    val tempA = currA
    currA = aItr.next
    (time, tab._2, tav._3, tempA)
  }
}.asInstanceOf[IndexedSeq[(Long, A, B, C)]]

On Saturday, January 7, 2017 at 10:13:07 AM UTC-6, Jason Nerothin wrote:
I'd like to 'zip'  a few datatypes: 

IndexedSeq[(Long,Foo)] and an IndexedSeq[(Long,Bar)]

case class Foo()
case class Bar()

val f = Foo()
val g = Foo()
val h = Foo()

val s = Bar()
val t = Bar()

val a = immutable.Vector((1,f),(2,g)(4,h),(7,f))
val b = immutable.Vector((1,s),(3,t),(9,t))

The result I want is: 

Seq[(1,f,s),(2,g,t),(4,h,t),(7,h,t)]

As you can see, in the case where there is a "miss in a", I want it to move up the next Bar of b, no matter how far away from a's index it happens to be. (A is dense, b is sparse. But their indices are relatively close to one another.) 


--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Loading...