How to make a function operate on a Sequence of Optional values?
Try this:
func firstValue<E, S: SequenceType where S.Generator.Element == Optional<E> >(seq: S) -> E? {
var g = seq.generate()
while let e:Optional<E> = g.next() {
if e != nil {
return e
}
}
return nil
}
let a:[Int?] = [nil,nil, 42, nil]
println(firstValue(a)) // -> 42 as Int?
I tested with Xcode Version 6.1.1 (6A2006) and Version 6.2 (6C86e)
Note
Without :Optional<E>
in while
condition, the compiler crashes.
And if we declare the function like this, the compiler clashes on some environment.
func firstValue<S: SequenceType, E where S.Generator.Element == Optional<E> > {
// ^^^^^^^^^^^^^^^^^^ replaced E and S
I think these are compiler bug. Please see the comments below.
python sequence order with optional values
You are actually only interested in the superset of the "optional" sequence. You can get your output by finding this superset and append each of its members to the "required" list.
from itertools import combinations
required = [1, 2]
optional = [3, 4]
superset_optional = [combinations(optional, i) for i in range(len(optional) + 1)]
for combinations in superset_optional:
for comb in combinations :
print(required + list(comb))
# [1, 2]
# [1, 2, 3]
# [1, 2, 4]
# [1, 2, 3, 4]
Scala: optional sequence of arguments
Make two functions:
def filteredSum(data: Seq[Int], filterValues: Int*): Int =
data.filter(filterValues.toSet).sum
def filteredSum(data: Seq[Int], all: Boolean) : Int =
if(all) data.sum else 0
how to pass optional parameters into a function in python?
You want something like this, I suppose:
def json_response_message(status, message, options=()):
data = {
'status': status,
'message': message,
}
# assuming options is now just a dictionary or a sequence of key-value pairs
data.update(options)
return data
And you can use it like this:
user = {
'facebook': 'fb',
'instagram': 'ig'
}
print(json_response_message(True, 'found', user))
Is there a better way to do optional function parameters in JavaScript?
Your logic fails if optionalArg is passed, but evaluates as false - try this as an alternative
if (typeof optionalArg === 'undefined') { optionalArg = 'default'; }
Or an alternative idiom:
optionalArg = (typeof optionalArg === 'undefined') ? 'default' : optionalArg;
Use whichever idiom communicates the intent best to you!
function as an optional parameter python
I try to guess what you want but this snippet might help you:
def fct(**kwargs):
if 'func' in kwargs:
f = kwargs['func']
return f(9)
else:
return 9
def f(x):
return x**2
print(fct()) # result = 9
print(fct(func=f)) # result = 81
Force a broader type for optional argument with more restrictive default value
OCaml simply does not support this. You cannot write a function with a type that is refined depending on whether an optional argument has been passed.
Unlike some the answers in the linked question, I agree that this is a reasonable thing to want to do. Indeed, typing things like Common Lisp's sequence functions (with :key
and :test
) seems to require something like this. However, OCaml is not a language in which it can be done.
The most reasonable approach is probably to write two functions, one of which takes an accessor as non-optional argument, and the other which supplies identity
as that argument:
let g f x = f x
let g_default x = g identity x
This is only a little clumsy, and you do not need to implement the logic in g
twice. However, applying this approach to combinations of more than one optional argument will become ugly.
Optional function parameter indication in UML Sequence Diagram
Optional parameters are nothing handled in UML as it's jus "syntactic sugar" compilers implement to make coders happy. You can mimic that in a SD with a note attached to the message. However, I just would not go to that detail and leave it to the coder (they aren't children you have to tell each single step to take).
javascript: optional first argument in function
You have to decide as which parameter you want to treat a single argument. You cannot treat it as both, content
and options
.
I see two possibilities:
- Either change the order of your arguments, i.e.
function(options, content)
Check whether
options
is defined:function(content, options) {
if(typeof options === "undefined") {
options = content;
content = null;
}
//action
}But then you have to document properly, what happens if you only pass one argument to the function, as this is not immediately clear by looking at the signature.
Related Topics
How to Detect When Two Objects Touch in Spritekit
Swift - Scanning with Ikscannerdeviceview on Osx
Multiple Scatterplots Using Core Plot and Swift
Error: Variable with Getter/Setter Cannot Have an Initial Value
Parse Nested Completion Handlers
Synchronous Request Using Alamofire
How to Get My Button to Reflect The Bool of My Environmentobject
Audiounit Callback and Synchronization: How to Ensure Thread Safety with Gcd
Add New Card Is Not Being Called in Stripe Paymentoptionviewcontroller
Realitykit - Keep Object Always in Front of Screen
Difference Between Any? and Any
Swiftui Concatenate Multiline Tappable Text
Optional Chaining and Array in Swift
I Am Loading Videos in Avplayer in Collection View But It Repeats Some Cells Data