Questions tagged [take]

Take is a common keyword or function name used to return a given number of elements, usually contiguously and from the beginning of a collection. Only use this tag for the take keyword or function name; usage such as "take a picture/screenshot" is out of scope.

Take functions usually only require two pieces of information: the number of elements to be returned from the sequence, and the sequence itself.

If take is a method in a particular library or language, it is called on the object and would only require the number parameter.

This concept is found in many languages and libraries, some of which are listed below.

Reference

142 questions
2
votes
4 answers

Is the take function well defined on a Scala Map?

Is the take function safe to use on a Scala Map? I thought that Maps were unordered, so that myMap.take(2) would return 2 random elements of myMap. But testing on the interpreter makes me feel that it is safe: scala> val z = Map(1 -> 10, 2 -> 20, 3…
ved
  • 297
  • 3
  • 11
2
votes
4 answers

Implement the take function with list comprehension

How would you implement take with a list comprehension? My approach so far: take2 :: (Num i, Ord i) => i -> [a] -> [a] take2 n xs = [x | x <- xs, [x..n]]
Michi
  • 41
  • 7
2
votes
1 answer

Haskell - How to take n items from a tuple list after sorting

Hi I am a beginner to haskell. I am trying to get the first 3 items from this tuple list: [("and",2),("cat",1),("dog",1),("rabbit",1),("the",2)] First I sort the list by frequency and descending order: sortWords = sortBy(flip compare `on`…
k1r4n
  • 69
  • 2
  • 12
2
votes
3 answers

Non-exhaustive patterns in function take6 error?

So i created this function , to give me the "n" first elements from a list,"(b:bs); 1 module Sexta where 2 3 take6::Int->[a]->[a] 4 take6 n (b:bs) = if n<=0 then [] 5 else [b] ++ (take6 (n-1) bs) The problem is that when…
Joao Parente
  • 443
  • 4
  • 8
2
votes
1 answer

Linq - Group By Id, Order By and Then select top 5 of each grouping

Is there some way in linq group By Id, Order By descending and then select top 5 of each grouping? Right now I have some code shown below, but I used .Take(5) and it obviously selects the top 5 regardless of grouping. Items = list.GroupBy(x =>…
Bad Dub
  • 1,503
  • 2
  • 22
  • 52
2
votes
1 answer

C# lambda limit with include

I have this piece of code. return Folder.GetAllWithInclude(x => x.SubFolder).Take(5); This code returns 5 Folder items. What I want to do is limit Subfolder to 5 instead of limiting Folder to 5. I have tried the following return…
2
votes
1 answer

Haskell - Use of Take and Drop to split a list into three parts

So I've came across this function definition that takes a list (xs) and splits it into three parts as an output. The thing is I'm having difficulty understanding it. I understand the first part which takes n of the list (xs) where n is defined as…
user5818725
2
votes
2 answers

making a takeUntil function in haskell

I want make a function which when given a string eg "ab" and "cdabd" when it will be used on these two strings it will output "cd" I have this up to now takeUntil :: String -> String -> String takeUntil [] [] = [] takeUntil xs [] = [] takeUntil []…
Nicholas
  • 125
  • 2
  • 12
2
votes
2 answers

LINQ: Skip and Take duplicates

I have an IQueryable containing items where everything is already grouped, so something like var animals = new List{"duck", "duck", "duck", "goose", "goose", "cow", "cow", "cow", "rabbit"}.AsQueryable(); If I do animals.Skip(2).Take(2); I…
Saining Li
  • 424
  • 2
  • 10
2
votes
1 answer

C# Custom Dictionary Take - Convert Back From IEnumerable

Scenario Having already read a post on this on the same site, which didn't work, I'm feeling a bit stumped but I'm sure I've done this before. I have a Dictionary. I want to take the first 200 values from the Dictionary. CODE …
Goober
  • 13,146
  • 50
  • 126
  • 195
2
votes
4 answers

Entity Framework - Linq : how query to take 2 first ordered and grouped list

I have a large table with 6000000 record like this format(Acc,sDate,Serial,Amount,...) Acc,date,serial is PKey. To show my problem, created small code public class Cheque { public string Account{ get; set; } public string Serial{ get; set;…
AliKarimi
  • 1,849
  • 1
  • 13
  • 8
2
votes
3 answers

Haskell List With Guards error

i am trying to write a very simple function in haskell to change a value in a list depending on an input as follows update_game :: [Int] -> Int -> Int -> [Int] update_game (x:xs) row take_amnt | row == 1 = x - take_amnt:xs …
Amman Vedi
  • 21
  • 1
2
votes
3 answers

groovy take how to reimplement

The take and drop methods were added to the list object in groovy v 1.8.1, and work like this: def list = ['Simple', 'list', 'with', 5, 'items'] assert list.take(2) == ['Simple', 'list'] I only have v 1.8.0 available to me. How can I…
cspeter8
  • 101
  • 5
1
vote
1 answer

Linq DataTable and Take Method

I have the following use of Linq from a datatable: var query = dt.AsEnumerable(); query = dt.AsEnumerable().Where(log => log.Field("Day") == day).Take(10); The following error: Cannot implicitly convert type …
Luke Wilkinson
  • 439
  • 8
  • 17
1
vote
0 answers

EF Core - Oracle Take and Skip

How to use Skip and Take with Oracle and EF Core? This query... var q = db.Set() .FilterBy(filtri) .Skip(0) .Take(10); ...Generates this wrong query (ora-00933 sql command not properly ended): DECLARE l_sql …
Max Bertoli
  • 614
  • 5
  • 25