Extractor composition with dependent sub-patterns - scala

Is it possible to define an extractor that is composed such that a sub-pattern depends on a previously matched sub-pattern?
Consider matching a date pattern, where valid "days" depends on the matched "month".
This is to avoid a guard to compare values bound by the sub-patterns, and also to avoid providing an overly-customized extractor.
Sample syntax:
case r"\d{4}-$month\d{2}-${day filter month.allows}\d{2}" => s"$month $day"

Perhaps you can formulate it under the aegis of this behavior:
https://issues.scala-lang.org/browse/SI-796
That is, before they fix it.

Related

Is there a simple way to filter & narrow collections on instance type in assertj?

Can this be written as a single line?
assertThat(actualDeltas)
.filteredOn(delta -> delta instanceof Replacement)
.asInstanceOf(InstanceOfAssertFactories.list(Replacement.class))
I expected asInstanceOf to do the filtering. Alternatively, I searched for extractors or other concepts, but couldn't find any simple solution.
Is that possible with assertj?
By design, the purpose of asInstanceOf is only to provide type-narrowed assertions for cases where the type of the object under assertion is not visible at compile time.
When you provide InstanceOfAssertFactories.list(Replacement.class) as a parameter for asInstanceOf, you are telling AssertJ that you expect the object under assertion to be a List with elements of type Replacement.
While asInstanceOf will make sure that the object under test is a List, it will neither filter nor enforce that all the list elements are of type Replacement. The Replacement will ensure type-safety with subsequent methods that can be chained, for example with extracting(Function).
Currently, filteredOn(Predicate) or any other filteredOn variant is the right way to take out elements that should not be part of the assertion. If the filtering would happen outside (e.g., via Stream API), no asInstanceOf call would be needed as assertThat() could detect the proper element type based on the input declaration.

How can I match either one pattern or another in Rosie?

How can I match one of two alternate patterns with Rosie Pattern Language?
For example if the string is "(248)" or "(313)" I want to match. Is this possible?
Yes this is possible. Given those two specific patterns the expression would look like this:
eitherAreaCode = "(248)"/"(313)"
This would match either (248) or (313) and nothing else.

Filter, Option or FlatMap in spark

I have next code in Spark:
dsPhs.filter(filter(_))
.map(convert)
.coalesce(partitions)
.filter(additionalFilter.IsValid(_))
At convert function I get more complex object - MyObject, so I need to prefilter basic object. I have 3 options:
Make map return option(MyObject) and filter it at additionalFilter
Replace map with flatMap and return empty array when filtered
Use filter before map function, to filter out RawObject, before converting it to MyObject.
Now I am go with option 3. But may be 1 or 2 is more preferable?
If, for option 2, you mean have convert return an empty array, there's another option: have convert return an Option[MyObject] and use flatMap instead of map. This has the best of options 1 and 2. Without knowing more about your use case, I can't say for sure whether this is better than option 3, but here are some considerations:
Should convert contain input validation logic? If so, consider modifying it to return an Option.
If convert is used, or will be used, in other places, could they benefit from this validation?
As a side note, this might be a good time to consider what convert currently does when passed an invalid argument.
Can you easily change convert and its signature? If not, consider using a filter.

Algolia search results for partial string matches

Trying to do a pretty basic search implementation of partial matching. For instance, I'd like 'ia hu' to return 'Ian Hunter'. I've got first and last name split so we're indexing first, last and combined.
Was reading the suggestion in here, but this just isn't a very elegant or feasible way to solve: https://www.algolia.com/doc/faq/troubleshooting/how-can-i-make-queries-within-the-middle-of-a-word.
I don't think we should have to generate a ton of substring combos for first and last name to get this to return results.
Has anyone implemented a more elegant solution?
In this specific use case (matching "Ian Hunter" with "ia hu"), you can turn prefix matching on all words with queryType=prefixAll (see documentation).
This will not allow infix matching, so "an hu" or "ia un" will not match "Ian Hunter". This cannot therefore be considered a general solution to your question. However, in practice, prefix matching tends to be what people use instinctively; infix matching is relatively rare in my experience.

Proper way to pattern match the value of a TypeTree from a ValDef in Scala Macros?

I need to read the fields of a case class and do different things depending on the field's type.
I thought I'd try with a macro, reading the ValDefs and pattern matching on the TypeTree of each, but that doesn't reveal what each TypeTree represents (e.g. Ints and Strings both appear as TypeTrees).
Is there an alternative to calling typeTree.toString and matching on the values of the Strings (e.g. "String" or "Int")?
You can use TypeTree's tpe method to see the underlying type.