I want to split an hot observable into multiple observable like follows
source: 'a' - 'b' - 'c' - 'd' - 'e' - 'a' - 'b' - 'c' - 'd' - 'd' - 'd - 'x' - '$' - 'z'......
output(array of observable):
'a' --------------------------'a'----------------......
----- 'b' -------------------------'b'-----------......
------------'c' ------------------------'c'------......
----------------- 'd' ---------------------'d'-'d'.....
-------------------------'e'---------------------......
groupBy doesn't work with hot observable.
partition only split an observable into 2 observables
I can make custom operator which use filter operator internally like partition operator. but it looks inefficient for my example because I have to apply filter operator many times to source.
Related
Hello I have a work when I have to do tuples of elements with parser but it gives me cyclic grammar error any way to do it proprely?
elements: | separated_nonempty_list(SEPARATOR_TOKEN,elements)
I'm looking for something like that:
elements (SEPARATOR_TOKEN) elements... (list)
Thank you
If your elements are either tuples (comma-separated lists) or single-characters terminals, "a,b,c,d" could be parsed as (a, (b, c, d)), (a, b, (c, d)), (a,(b,(c,(d)))), ((((a),b),c),d) etc., where I marked the tuples with parentheses.
It is a cyclic grammar error because in order to parse a tuple, you may need to parse a tuple first (you could parse it if you had a backtracking mechanism in place where you want to try different ways of parsing a string, but in your case you want a deterministic grammar).
You should have another non-terminal named for example atom (or atomic_element), and make sure your elements can be a list of atoms.
If you want elements to recursively contain elements, you have to add a layer of disambiguation with e.g. parentheses:
atom : <single-letter>
| '(' elements ')'
I'm completely new to functional programming and have elected to use F# for a project which entails the parsing and minimization of a DFA.
I currently have my parser completed and am able to format each element of the DFA's tuple (states, alphabet, transition function, start state, final states) in whatever way I'd like and I have reached the point where I need to implement the minimization algorithm. The algorithm being used is:
For some DFA (Q, Σ, δ, S, F) where
Q: The set of states
Σ: The alphabet
δ: The transition function
S: The start state
F: The set of final states
Step 1. For each pair of states
(p, q) ∈ Q x Q
If p ∈ F and q ∉ F (or vice versa), then set distinct(p, q) = 1.
Step 2. Loop until there is no change in the table contents:
For each pair of states (p, q) ∈ Q x Q:
For each alphabet symbol a ∈ alphabet:
If distinct(p, q) = 0 and distinct(δ(p, a), δ(q, a)) = 1, then set
distinct(p, q) = 1.
I have the DFA tuple elements formatted like so:
States:
["0";"1";"2";"3"]
Alphabet:
["a";"b"]
Transition Function (ie: ["0";"a";"1"] is read as "0 on an 'a' goes to 1"]
[["0";"a";"1"];["1";"a";"1"];["1";"b";"2"];["2";"a";"0"];...;["5";"a";"4"]
Start State:
["0"]
Final States:
["1";"5"]
I also have have a distinct table formatted. It's basically the Cartesian product of States x States (QxQ from the above minimization algorithm) with any repeated products and duplicate elements ignored:
[["0";"1"];["0";"2"];["0";"3"];["0";"4"];["0";"5"];["1";"2"];
["1";"3"];["1";"4"];["1";"5"];["2";"3"];["2";"4"];["2";"5"];
["3";"4"];["3";"5"];["4";"5"]]
My initial strategy was to make a new list with only pairs which are either both non-final, or both final. (The only two conditions failing the 'Step 1' condition).
My problem is this: I am having a difficult time coming up with a way to compare the resulting list to the transition function of each pair of states. For example, take the pair of states ["1";"5"]. As the algorithm states, we must compare what happens to '1' for each alphabet character to what happens to '5' for each alphabet character. In this case, the transition function states:
For 1:
["1";"a";"1"];["1";"b";"2"]
-'1' on an 'a' goes to '1'
-'1' on a 'b' goes to '2'
And for 5:
["5";"a";"4"]
-'5' on an 'a' goes to '4'
Because both states, '5' and '1', behave differently when passed the same alphabet character, they are distinct. But, as I've stated, I'm not at all clear as to how to implement this comparison.
Any help would be greatly appreciated. Take care!
If your transition function is stored as triples as you show above:
Sort them into separate lists of state pairs according to letter
Make a random access array (or associative map) out of each such list
Since you are inverting the transition function, the index/key of each mapping will be the destination state, and the contents/value will be a list of zero or more origin states that the letter maps to it.
Make sure that all elements of your "distinct" list have their first element less than their second (by swapping the two if necessary), and make sure the list itself is sorted. Then, for each array or map:
Apply the mapping to your "distinct" list as follows:
look up both elements of each "distinct" pair
perform the cartesian product of the two "origin state" lists from a given pair
combine the resulting pairs from all cartesian products into a single list
For this new list:
Filter out any resulting tuples with identical elements
Swap any tuples whose first element is greater than the second
Sort the result
Eliminate neighboring duplicates
Do a merge pass with the "distinct" list:
I have started learning Erlang recently and came across the following error while trying to pattren match
The following expression is working fine:
{A,_,[B|_],{B}}={abc,23,[22,x],{22}}.
Resulting in
A = abc
B = 22
The following expression is not working:
{A,_,[_|B],{B}}={abc,23,[22,x],{x}}.
Is resulting in
** exception error: no match of right hand side value {abc,23,[22,x],{x}}
However if I replace the ',' in [22 , x with a | like the following its working find and bounding x to B
{A,_,[_|B],{B}}={abc,23,[22|x],{x}}.
{abc,23,[22|x],{x}}
B.
x
Any explanation about this would highly appreciated.
Many thanks in advance
The operator | is used for a recursive definition of a list: [A|B] means that you add the element A to an existing list B. A is the first element of the resulting list, called the head, B is the rest of the list called tail. B can be also split into a head and a tail, and the process can continue until the tail is equal to the empty list [].
The operator , is a separator between list elements, so [A,B] is a list of 2 elements A and B.
The 2 operators can be combined: [A,B,C|D] is a list of at least 3 elements, which are A, B and C, and a tail D which can be empty.
In your test you used another syntax: [23|x]; 23 can be an element of a list (in fact any erlang term can be an element of a list) but x is an atom and cannot be a list tail. Doing this you broke the recursive definition of the list, this structure is not often used and is called an improper list.
when you match [_|B] and [_,x], you assign [x] to B which do not match to x later in the expression
when you match [_|B] and [_|x], you assign x to B which indeed match to x later in the expression, but the right way should be
{A,_,[_|B],{B}}={abc,23,[22,x],{[x]}}.
You need to look closer on how does the | operator works. It basically takes head of list, which is one element, and returns tail of list, which is all the rest. And like "all" suggest tail is also a list. It could be one element list, it could be even empty list, but still it's gonna be a list.
> [Head| Tail] = [23,x].
[23,x]
> Head.
23
> Tail
[x].
So in your pattern matching, you assign to be tail [x], and than try to pattern match on simply x. And that's what's failing.
On side note: you can create new list with | operator, but you should do this with caution. since you could create improper list (and you do with [23 | x]). That's why your "fix" is working.
If you would like to match on two element list, you could do it explicitly with
[A, B] = [23, x].
but this will fail if list have more or less elements.
If you would like to match on only on two first elements, you can still use | operator.
> [A, B | Rest] = [23, x].
[23, x]
> A.
23
> B.
x
> Rest.
[].
And this will fail only with one-element or empty list.
In a normal PEG (parsing expression grammar) this is a valid grammar:
values <- number (comma values)*
number <- [0-9]+
comma <- ','
However, if I try to write this using LPeg the recursive nature of that rule fails:
local lpeg = require'lpeg'
local comma = lpeg.P(',')
local number = lpeg.R('09')^1
local values = number * (comma * values)^-1
--> bad argument #2 to '?' (lpeg-pattern expected, got nil)
Although in this simple example I could rewrite the rule to not use recursion, I have some existing grammars that I'd prefer not to rewrite.
How can I write a self-referencing rule in LPeg?
Use a grammar.
With the use of Lua variables, it is possible to define patterns incrementally, with each new pattern using previously defined ones. However, this technique does not allow the definition of recursive patterns. For recursive patterns, we need real grammars.
LPeg represents grammars with tables, where each entry is a rule.
The call lpeg.V(v) creates a pattern that represents the nonterminal (or variable) with index v in a grammar. Because the grammar still does not exist when this function is evaluated, the result is an open reference to the respective rule.
A table is fixed when it is converted to a pattern (either by calling lpeg.P or by using it wherein a pattern is expected). Then every open reference created by lpeg.V(v) is corrected to refer to the rule indexed by v in the table.
When a table is fixed, the result is a pattern that matches its initial rule. The entry with index 1 in the table defines its initial rule. If that entry is a string, it is assumed to be the name of the initial rule. Otherwise, LPeg assumes that the entry 1 itself is the initial rule.
As an example, the following grammar matches strings of a's and b's that have the same number of a's and b's:
equalcount = lpeg.P{
"S"; -- initial rule name
S = "a" * lpeg.V"B" + "b" * lpeg.V"A" + "",
A = "a" * lpeg.V"S" + "b" * lpeg.V"A" * lpeg.V"A",
B = "b" * lpeg.V"S" + "a" * lpeg.V"B" * lpeg.V"B",
} * -1
It is equivalent to the following grammar in standard PEG notation:
S <- 'a' B / 'b' A / ''
A <- 'a' S / 'b' A A
B <- 'b' S / 'a' B B
I know this is a late answer but here is an idea how to back-reference a rule
local comma = lpeg.P(',')
local number = lpeg.R('09')^1
local values = lpeg.P{ lpeg.C(number) * (comma * lpeg.V(1))^-1 }
local t = { values:match('1,10,20,301') }
Basically a primitive grammar is passed to lpeg.P (grammar is just a glorified table) that references the first rule by number instead of name i.e. lpeg.V(1).
The sample just adds a simple lpeg.C capture on number terminal and collects all these results in local table t for further usage. (Notice that no lpeg.Ct is used which is not a big deal but still... part of the sample I guess.)
I'm writting a grammar for arithmetic expression in prefix notation. However I have an issue when parsing negative numbers or substraction. Grammar example is this:
precedence right +, -;
precedence right *, /;
precedence right uminus;
E ::= + E E
| - E E
| * E E
| / E E
| ( E )
| - E %prec uminus
| id
| digit
;
But if my input is - 5 4, it reduces 5 as E, next it reduces - E (negative) and then parser gives me a syntax error at 4. The correct one should be 5 as E, next 4 as E and then - E E as E. How can I solve this problem using associativity? or do I need to rewrite my grammar?
(Promoted from comment)
Your grammar really is ambiguous, and precedence declarations won't help you a bit.
Consider the input the input consisting of N - tokens, followed by M 1 tokens.
- - - - - - - ... - 1 1 1 ... 1
In order for this to be an expression, M-1 of the - tokens must be binary, and the remaining N-(M-1) unary, but there is no way to tell which is which (unless they are all binary).
Even if you arbitrarily say that the first N-(M-1) -s are unary, you can't tell what the value of N-(M-1) is until you read the entire input, which means you can't parse with a finite lookahead.
But the whole point of prefix notation is to avoid the need for parentheses. Arbitrary declarations like the above make it impossible to represent alternative interpretations, so that some expressions would be impossible to represent in prefix notation. That's just plain wrong.
Here's a simple case:
- 5 - - - 4 3 1
is either
5 - (- (4 - (3 - 1)))
5 - ((- (4 - 3)) - 1)
5 - (((- 4) - 3) - 1)
In prefix notation, you need to declare the "arity" of every operator, either implicitly (every operator has a known number of arguments), or explicitly using a notation like this, borrowed from Prolog:
-/2 5 -/2 -/2 -/1 4 3 1
Alternatively, you can delimit the arguments with mandatory parentheses, as with Lisp/Scheme "s-exprs":
(- 5 (- (- (- 4) 3) 1))
In first place, remove all precedence declarations. They are not needed in prefix grammars. In fact, that should be enough to solve the issue in any parser generator. Which one are you using, BTW?
Cup has a finite lookahead. As #rici points out, the ambiguity can't be resolved in this case. What you can do is to restrict the grammar so just one consecutive unary - can be used.
B ::= E
| - E
;
E ::= + B B
| - B B
| * B B
| / B B
| ( B )
| id
| digit
;
Please check the above several times as I'm pretty rusty.