by crazypython · 6y ago · 0 comments
seperator.join(iterable)
"".join(["foo", "bar", "hello"]) # => "foobarhello"
", ".join(["Hacker News", "Twitter"]) # => "Hacker News, Twitter
Many people have complained about this strange syntax:https://mail.python.org/archives/list/python-ideas@python.or... https://mail.python.org/archives/list/python-ideas@python.or... https://mail.python.org/archives/list/python-ideas@python.or... https://mail.python.org/archives/list/python-ideas@python.or...
`["a", "b", "c"].join(iterable)` was rejected because it forces every iterable to implement iterable functions.
Additionally some iterables, like generators, are mutated when you iterate over them:
", ".join(streaming_api.fetch_names())
UFCS let's you write join as a free function and still use "".join(iterable)NaN guarding:
import std.traits;
auto unwrap(T)(T input) if (isFloatingPoint!T) {
// Asserts cost nothing when turned off with -check=assert=off
// The compiler can fully inline this function, so it isn't expensive
assert(input !== input.nan);
return input;
}
Use like this: if (objSize.unwrap < cellSize) {
// Allocate a new cell for the object
} else {
// Insert into current cell
}
The parentheses are omitted because Dlang has https://dlang.org/spec/function.html#optional-parenthesis. The above compiles into this: if (objSize.unwrap() < cellSize) {
// Allocate a new cell for the object
} else {
// Insert into current cell
}No comments yet.