splat

Unroll / splat arguments in common lisp

天涯浪子 提交于 2021-02-04 17:13:30
问题 Say I have a list of arguments: > (setf format-args `(t "it's ~a" 1)) (T "it's ~a" 1) How can I then "splat" or "unroll" this into a series of arguments rather than a single list argument, for supplying to the format function? i.e I would like the following function call to take place: > (format t "it's ~a" 1) For reference, I would write the following in python or ruby: format(*format-args) I'm sure it can be done, but perhaps I'm thinking about it wrong. It also doesn't help that the name

Does Haskell have a splat operator like Python and Ruby?

余生长醉 提交于 2020-01-29 04:32:06
问题 In Python and Ruby (and others as well, I'm sure). you can prefix an enumerable with * ("splat") to use it as an argument list. For instance, in Python: >>> def foo(a,b): return a + b >>> foo(1,2) 3 >>> tup = (1,2) >>> foo(*tup) 3 Is there something similar in Haskell? I assume it wouldn't work on lists due to their arbitrary length, but I feel that with tuples it ought to work. Here's an example of what I'd like: ghci> let f a b = a + b ghci> :t f f :: Num a => a -> a -> a ghci> f 1 2 3 ghci

two level splatter TCL

半世苍凉 提交于 2020-01-25 19:01:05
问题 If I have a procedure or a command in TCL, with variable number of arguments, one can use, if a list's elements are as an input, the "splatter" operator, for example: set a [list "ko" ] set m [ list "ok" "bang" ] lappend a {*}$m But, what if I want to "twice splatter"? I.e., flatten 2 levels? Using it twice, in sequence, does not work: set a [list "ko" ] set m [ list [ list "ok" ] [ list "bang" ] ] lappend a {*}{*}$m Will error out on extra character. 回答1: You've noticed that {*}

Best way to document “splatted” parameter with YARD? [closed]

江枫思渺然 提交于 2019-12-30 06:17:31
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed last year . I have a method that should take 1+ parameters of any class, similar to Array#push: def my_push(*objects) raise ArgumentError, 'Needs 1+ arguments' if objects.empty? objects.each do |obj| puts "An object was pushed: #{obj.inspect}" @my_array.push obj end end What is the best

What does the * (asterisk) symbol do near a function argument and how to use that in others scenarios?

风流意气都作罢 提交于 2019-12-30 01:51:29
问题 I am using Ruby on Rails 3 and I would like to know what means the presence of a * operator near a function argument and to understand its usages in others scenarios. Example scenario (this method was from the Ruby on Rails 3 framework): def find(*args) return to_a.find { |*block_args| yield(*block_args) } if block_given? options = args.extract_options! if options.present? apply_finder_options(options).find(*args) else case args.first when :first, :last, :all send(args.first) else find_with

What's the splat doing here?

不打扰是莪最后的温柔 提交于 2019-12-23 16:53:30
问题 match, text, number = *"foobar 123".match(/([A-z]*) ([0-9]*)/) I know this is doing some kind of regular expression match but what role does the splat play here and is there a way to do this without the splat so it's less confusing? 回答1: is there a way to do this without the splat so it's less confusing? Since a,b = [c,d] is the same as a,b = *[c,d] and splat calls to_a on its operand when it's not an array you could simply call to_a explicitly and not need the splat: match, text, number =

Ruby rubocop: how to freeze an array constant generated with splat

末鹿安然 提交于 2019-12-23 12:55:41
问题 I'm assigning an array constant like this: NUMS = *(2..9) Rubocop says C: Freeze mutable objects assigned to constants. NUMS = *(2..9) ^^^^^ So I try NUMS = *(2..9).freeze Rubocop says C: Freeze mutable objects assigned to constants. NUMS = *(2..9).freeze ^^^^^^^^^^^^ Tried NUMS = (*(2..9)).freeze Rubocop says E: unexpected token tRPAREN (Using Ruby 2.0 parser; configure using TargetRubyVersion parameter, under AllCops) NUMS = (*(2..9)).freeze ^ Tried NUMS = [1, 2, 3, 4, 5, 6, 7, 8, 9].freeze

Splat on a hash

最后都变了- 提交于 2019-12-23 07:26:38
问题 A splat on a hash converts it into an array. [*{foo: :bar}] # => [[:foo, :bar]] Is there some hidden mechanism (such as implicit class cast) going on here, or is it a built-in primitive feature? Besides an array, are nil and hash the only things that disappear/change with the splat operator under Ruby 1.9? 回答1: A splat will attempt an explicit conversion of an object to an Array. To do this, it will send to_a and expect an Array as a result. class Foo def to_a [1,2,3] end end a, b, c = *Foo

Why is the splat used inside an array definition here?

試著忘記壹切 提交于 2019-12-19 03:35:30
问题 def initialize(apps, catch=404) @apps = []; @has_app = {} apps.each { |app| add app } @catch = {} [*catch].each { |status| @catch[status] = true } end In this method from Rack::Cascade, what purpose does the splat(*) serve in the [*catch] code? I thought a splat was used in method arguments to indicate when you are going to have an unspecified number of parameters. Does the splat have a different meaning here? 回答1: It creates a single flat array for catch I'm not sure anyone completely

Why do Ruby procs/blocks with splat arguments behave differently than methods and lambdas?

别来无恙 提交于 2019-12-18 16:46:22
问题 Why do Ruby (2.0) procs/blocks with splat arguments behave differently than methods and lambdas? def foo (ids, *args) p ids end foo([1,2,3]) # => [1, 2, 3] bar = lambda do |ids, *args| p ids end bar.call([1,2,3]) # => [1, 2, 3] baz = proc do |ids, *args| p ids end baz.call([1,2,3]) # => 1 def qux (ids, *args) yield ids, *args end qux([1,2,3]) { |ids, *args| p ids } # => 1 Here's a confirmation of this behavior, but without explanation: http://makandracards.com/makandra/20641-careful-when