Chart of IEnumerable LINQ equivalents in Scala?

"YOU AND THE ART OF ONLINE DATING" is the only product on the market that will take you step-by-step through the process of online dating, provide you with the resources to help ensure success. Get it now!

I am only listing out the equivalents of functions from Enumerable.

I am only listing out the equivalents of functions from Enumerable. This is incomplete as of now. I will try to update this later with more.Xs.

Aggregate(accumFunc) -> xs. ReduceLeft(accumFunc) xs. Aggregate(seed, accumFunc) -> xs.

FoldLeft(seed)(accumFunc) xs. Aggregate(seed, accumFunc, trans) -> trans(xs. FoldLeft(seed)(accumFunc)) xs.

All(pred) -> xs. Forall(pred) xs.Any() -> xs. NonEmpty xs.

Any(pred) -> xs. Exists(pred) xs.AsEnumerable() -> xs. AsTraversable // roughly xs.Average() -> xs.

Sum / xs. Length xs. Average(trans) -> trans(xs.

Sum / xs. Length) xs.Cast() -> xs. Map(_.

AsInstanceOfA) xs. Concat(ys) -> xs ++ ys xs. Contains(x) -> xs.

Contains(xs) xs. Contains(x, eq) -> xs. Exists(eq(x, _)) xs.Count() -> xs.

Size xs. Count(pred) -> xs. Count(pred) xs.DefaultIfEmpty() -> if(xs.

IsEmpty) List(0) else xs // Use `mzero` (from Scalaz) instead of 0 for more genericity xs. DefaultIfEmpty(v) -> if(xs. IsEmpty) List(v) else xs xs.Distinct() -> xs.

Distinct xs. ElementAt(i) -> xs(i) xs. ElementAtOrDefault(i) -> xs.

Lift(i). OrZero // `orZero` is from Scalaz xs. Except(ys) -> xs.

Diff(ys) xs.First() -> xs. Head xs. First(pred) -> xs.

Find(pred) // returns an `Option` xs.FirstOrDefault() -> xs.headOption. OrZero xs. FirstOrDefault(pred) -> xs.

Find(pred). OrZero xs. GroupBy(f) -> xs.

GroupBy(f) xs. GroupBy(f, g) -> xs. GroupBy(f).

MapValues(_. Map(g)) xs. Intersect(ys) -> xs.

Intersect(ys) xs.Last() -> xs. Last xs. Last(pred) -> xs.reverseIterator.

Find(pred) // returns an `Option` xs.LastOrDefault() -> xs.lastOption. OrZero xs. LastOrDefault(pred) -> xs.reverseIterator.

Find(pred). OrZero xs.Max() -> xs. Max xs.

Max(f) -> xs. MaxBy(f) xs.Min() -> xs. Min xs.

Min(f) -> xs. MinBy(f) xs.OfType() -> xs. Collect { case x: A => x } xs.

OrderBy(f) -> xs. SortBy(f) xs. OrderBy(f, comp) -> xs.

SortBy(f)(comp) // `comp` is an `Ordering`. Xs. OrderByDescending(f) -> xs.

SortBy(f)(implicitlyOrderingA. Reverse) xs. OrderByDescending(f, comp) -> xs.

SortBy(f)(comp. Reverse) Enumerable. Range(start, count) -> start until start + count Enumerable.

Repeat(x, times) -> Iterator. Continually(x). Take(times) xs.Reverse() -> xs.

Reverse xs. Select(trans) -> xs. Map(trans) // For indexed overload, first `zipWithIndex` and then `map`.

Xs. SelectMany(trans) -> xs. FlatMap(trans) xs.

SequenceEqual(ys) -> xs. SameElements(ys) xs. Skip(n) -> xs.

Drop(n) xs. SkipWhile(pred) -> xs. DropWhile(pred) xs.Sum() -> xs.

Sum xs. Sum(f) -> xs. Map(f).

Sum // or `xs. FoldMap(f)`. Requires Scalaz.

Xs. Take(n) -> xs. Take(n) xs.

TakeWhile(pred) -> xs. TakeWhile(pred) xs. OrderBy(f).

ThenBy(g) -> xs. SortBy(x => (f(x), g(x))) // Or: xs. SortBy(f &&& g).

`&&&` is from Scalaz. Xs.ToArray() -> xs. ToArray // Use `xs.

ToIndexedSeq` for immutable indexed sequence. Xs. ToDictionary(f) -> xs.

Map(f. First). ToMap // `first` is from Scalaz.

When f = identity, you can just write `xs. ToMap`.Xs.ToList() -> xs. ToList // This returns an immutable list.

Use `xs. ToBuffer` if you want a mutable list. Xs.

Union(ys) -> xs. Union(ys) xs. Where(pred) -> xs.

Filter(pred) xs. Zip(ys, f) -> (xs, ys).zipped. Map(f) // When f = identity, use `xs.

Zip(ys)`. There is no direct equivalent of some functions, but it's fairly easy to roll your own. Here are some such functions.

Single: def singleA(xs: TraversableA): A = { if(xs. IsEmpty) sys error "Empty sequence! " else if(xs.

Size > 1) sys error "More than one elements!" else xs. Head } SingleOrDefault: def singleOrDefaultA : Zero(xs: TraversableA): A = { if(xs. IsEmpty) mzero else if(xs.

Size > 1) sys error "More than one elements! " else xs. Head } Join: def joinA, B, K, R(outer: TraversableA, inner: TraversableB) (outKey: A => K, inKey: B => K, f: (A, B) => R): TraversableR = { for(o K, inKey: B => K, f: (A, TraversableB) => R): TraversableR = { for(o 1) Left(new RuntimeException("More than one elements!")) else Right(xs.

Head) } Scalaz's Zero/mzero are not quite same as C#'s default value mechanism. For details, you can refer to this post I wrote on this topic some time back. You can use pimp-my-library pattern to achieve the same effect as C#'s extension methods.

Refer to this and this for details.

Thank you VERY much! When updating, if there is no 1:1 mapping, please just put it as "no 1:1" mapping, thank you in advance. – macias Nov 12 '11 at 20:30 The third Aggregate is incorrect.

Trans(xs. FoldLeft(seed)(accumFunc)) is proper. – Daniel C.

Sobral Nov 12 '11 at 21:49 @missingfaktor: Would it be possible to use that list for docs.scala-lang.org? – soc Nov 12 '11 at 23:26 start to start + count - 1 => start until start + count – soc Nov 12 '11 at 23:35 @DanielC. Sobral: Thanks for the correction!

– missingfaktor Nov 12 '117 at 5:04.

Scala> val l = List(1, 2, 3, 4, 5) l: ListInt = List(1, 2, 3, 4, 5) scala> l. Head res0: Int = 1 scala> l. HeadOption res1: OptionInt = Some(1) scala> l.

Map(_. ToString) res2: Listjava.lang. String = List(1, 2, 3, 4, 5) scala> l(1) res3: Int = 2 There's no method to get an element or a default, but this will work: scala> scala.util.control.Exception.allCatch.

Opt(l(5)) getOrElse 0 res4: Int = 0.

Thank you but I am looking for complete LINQ -> Scala translation, so I could mentally be on right track faster. Something to print, read and memorize. – macias Nov 12 '11 at 16:05 1 For that last one, you could have done l.

Lift(5). GetOrElse(0). – missingfaktor Nov 12 '11 at 18:05.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions