4 ms·
Joining an iterable of strings into a single string. Python does: seperator.join(iterable) "".join(["foo", "bar", "hello"]) # => "foobarhello" ", ".join(
by crazypython 6y ago
Joining an iterable of strings into a single string. Python does:
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.org/thread/LOJEKT2BIGGYXTR7GUBL3BMX2GZUAEEK/#QWJNKMI4KAZUVPDJTHYOHC65GQ7ILJSV https://mail.python.org/archives/list/python-ideas@python.or...
https://mail.python.org/archives/list/python-ideas@python.org/thread/JNBEZSOWDWPQGQ6A2EXM32FGKYXVRHZM/#V7DEV5W6SXJWRD65UKNMOWWQOUIGMLYH https://mail.python.org/archives/list/python-ideas@python.or...
https://mail.python.org/archives/list/python-ideas@python.org/thread/JNBEZSOWDWPQGQ6A2EXM32FGKYXVRHZM/#DXIZW67RUGRUYJERYFTRRSK3I4T4DNYU https://mail.python.org/archives/list/python-ideas@python.or...
https://mail.python.org/archives/list/python-ideas@python.org/thread/LOJEKT2BIGGYXTR7GUBL3BMX2GZUAEEK/#YOGIBUP5OJEGZJO2YKNDH4NHPAEIFBS4 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 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
}