tuples

Showing a Haskell list of tuples with custom syntax

点点圈 提交于 2019-12-20 04:22:47
问题 I have a list of tuples [(1,'a','%',"yes"),(2,'b','[',"no"),(3,'c',']',"ok")] . How can I show this list as output in the form of [(1,a,%,yes),(2,b,[,no),(3,c,],ok)] ? 回答1: Looks like the transformation you wish to make is to strip out quote characters? If so, filtering the results of calling show on your data will be enough: > let x = [(1,'a','%',"yes"),(2,'b','[',"no"),(3,'c',']',"ok")] Then apply a filter, > putStrLn . filter (`notElem` "'\"") . show $ x [(1,a,%,yes),(2,b,[,no),(3,c,],ok)]

Create a single element tuple of tuple

五迷三道 提交于 2019-12-20 03:33:18
问题 I just noticed that if you want to create a tuple with single element, being a tuple itself, you cannot do it with constructor tuple only with (,) syntax. Why is that? Example: >>> tuple(list('abc')) ('a', 'b', 'c') >>> tuple(tuple(list('abc'))) ('a', 'b', 'c') >>> (tuple(list('abc')),) (('a', 'b', 'c'),) But then it holds for a list >>> tuple([1],) (1,) >>> tuple([1]) (1,) 回答1: I don't really see the issue, this adheres to the documentation: class tuple(object) | tuple() -> empty tuple |

Confusion in understanding tuple and *args in Python

我的未来我决定 提交于 2019-12-20 03:09:58
问题 I need a function which would be taking variadic arguments . The number of arguments may vary from 1 to N. def abc(*args): print "ABC" print args print len(args) def abc1(*args): print "ABC1" print args print len(args) print "------------" tup = ("Hello123") abc(*tup) abc1(tup) tup = ("Hello123", "Hello1234") abc(*tup) abc1(tup) The ouput of the above program is; ABC ('H', 'e', 'l', 'l', 'o', '1', '2', '3') 8 ABC1 ('Hello123',) 1 ------------ ABC ('Hello123', 'Hello1234') 2 ABC1 (('Hello123',

Can I avoid template recursion here?

半城伤御伤魂 提交于 2019-12-20 01:14:47
问题 I've written a for_each for tuple s: template <typename Tuple, typename F, size_t begin, size_t end> enable_if_t<begin == end || tuple_size<Tuple>::value < end> for_each(Tuple&, F&&) { } template <typename Tuple, typename F, size_t begin = 0U, size_t end = tuple_size<Tuple>::value> enable_if_t<begin < end && tuple_size<Tuple>::value >= end> for_each(Tuple& t, F&& f) { f(get<begin>(t)); for_each<Tuple, F, begin + 1, end>(t, forward<F>(f)); } [Live Example] But Yakk's answer to this question

Processing tuples in java

ⅰ亾dé卋堺 提交于 2019-12-20 01:10:03
问题 I am processing some data with the following format: String s = "{(30,2884090,1410450570357,235),(30,2863348,1410451100148,285)}" Some doubts beset me: Are there two entries (tuples) in this String? Is there any off-the-shelf data structure I can use to parse this? Is there any way to figure out a pattern matching which can return a list of two Strings for the given String ? 回答1: As far as I know, Java API does not have something that can be used out-of-box. You need to write a small parser

What is the best way to check if a variable is a list? [duplicate]

我的梦境 提交于 2019-12-20 00:59:39
问题 This question already has answers here : What are the differences between type() and isinstance()? (7 answers) Closed 2 years ago . I found this 3 ways to check it, but I don't know which of them is the best: x = ['Bla', 'Bla', 'Bla', 'etc'] if isinstance(a, list): print('Perfect!') if type(a) is list: print('Incredible!') if type(a) == type([]): print('Awesome!') Which of these is better? Also, Can I use these ways to check whether an x is a string, tuple, dictionary, int, float, etc? If

How do I check if an array of tuples contains a particular one in Swift?

ⅰ亾dé卋堺 提交于 2019-12-19 19:56:24
问题 Consider the following Swift code. var a = [(1, 1)] if contains(a, (1, 2)) { println("Yes") } All I need is to check if a contains the tuple but the code leads to error. Cannot find an overload for 'contains' that accepts an argument list of type '([(Int, Int)], (Int, Int))' Why so and how to use contains properly? 回答1: Add the following to your code: func contains(a:[(Int, Int)], v:(Int,Int)) -> Bool { let (c1, c2) = v for (v1, v2) in a { if v1 == c1 && v2 == c2 { return true } } return

Invoke a method using a tuple as the parameter list [duplicate]

旧城冷巷雨未停 提交于 2019-12-19 14:57:49
问题 This question already has answers here : scala tuple unpacking (5 answers) Closed 4 years ago . I wonder what is the best way to do this. val foo = Some("a") val bar = Some(2) def baz(a: String, b: Int) = if((b % 2) == 0) Some(a+","+b) else None (x zip y) flatMap baz //does not compile of course (x zip y) flatMap { x => baz(x._1, x._2) } //ugly I would presume that Odersky et al. have another trick up theirs sleeve to reduce the noise in this example. So the question is how to fight the

Invoke a method using a tuple as the parameter list [duplicate]

偶尔善良 提交于 2019-12-19 14:56:12
问题 This question already has answers here : scala tuple unpacking (5 answers) Closed 4 years ago . I wonder what is the best way to do this. val foo = Some("a") val bar = Some(2) def baz(a: String, b: Int) = if((b % 2) == 0) Some(a+","+b) else None (x zip y) flatMap baz //does not compile of course (x zip y) flatMap { x => baz(x._1, x._2) } //ugly I would presume that Odersky et al. have another trick up theirs sleeve to reduce the noise in this example. So the question is how to fight the

Why is there no Tuple1 Literal for single element tuples in Scala?

六月ゝ 毕业季﹏ 提交于 2019-12-19 12:53:15
问题 Python has (1,) for a single element tuple. In Scala, (1,2) works for Tuple2(1,2) but we must use Tuple1(1) to get a single element tuple. This may seem like a small issue but designing APIs that expect a Product is a pain to deal for users that are passing single elements since they have to write Tuple1(1). Maybe this is a small issue, but a major selling point of Scala is more typing with less typing. But in this case it seems it's more typing with more typing. Please tell me: 1) I've