Simple Examples of Filter Function, Recursive Option Specifically

simple examples of filter function, recursive option specifically

In the recursive case, I think no need to expand the expression in terms of xi.
The key with "recursive" is to express the right hand expression in terms of previous y's.

I prefer thinking in terms of filter size.

filter size =1

y1 <- x1                                            
y2 <- x2 + f1*y1
y3 <- x3 + f1*y2
y4 <- x4 + f1*y3
y5 <- x5 + f1*y4

filter size = 2

y1 <- x1                                            
y2 <- x2 + f1*y1
y3 <- x3 + f1*y2 + f2*y1 # apply the filter for the past value and add current input
y4 <- x4 + f1*y3 + f2*y2
y5 <- x5 + f1*y4 + f2*y3

Recursively filter array of objects

Using .filter() and making a recursive call as I described in the comment above is basically what you need. You just need to update each .children property with the result of the recursive call before returning.

The return value is just the .length of the resulting .children collection, so if there's at least one, the object is kept.

var res = input.filter(function f(o) {
if (o.value.includes("Hit")) return true

if (o.children) {
return (o.children = o.children.filter(f)).length
}
})

const input = [

{

value: 'Miss1',

children: [

{ value: 'Miss2' },

{ value: 'Hit1', children: [ { value: 'Miss3' } ] }

]

},

{

value: 'Miss4',

children: [

{ value: 'Miss5' },

{ value: 'Miss6', children: [ { value: 'Hit2' } ] }

]

},

{

value: 'Miss7',

children: [

{ value: 'Miss8' },

{ value: 'Miss9', children: [ { value: 'Miss10' } ] }

]

},

{

value: 'Hit3',

children: [

{ value: 'Miss11' },

{ value: 'Miss12', children: [ { value: 'Miss13' } ] }

]

},

{

value: 'Miss14',

children: [

{ value: 'Hit4' },

{ value: 'Miss15', children: [ { value: 'Miss16' } ] }

]

},

];

var res = input.filter(function f(o) {

if (o.value.includes("Hit")) return true

if (o.children) {

return (o.children = o.children.filter(f)).length

}

})

console.log(JSON.stringify(res, null, 2))

How does filter function in R works

According to the ?stats::filter, if we specify the method as "recursive" an autoregression is used and the filter argument takes a vector of filter coefficients in reverse time order. The recursive filter is based on

Sample Image

So, using the same principle, we multiply the filter coefficient i.e. 0.5 with the previous value and add with the current value

x1 <- x[1]
x2 <- x[2] + 0.5 * x1
x2
#[1] 2.5
x3 <- x[3] + 0.5 * x2
x3
#[1] 4.25

Working of stats::filter function R

The filter is applied in reverse time order. So the first element of the second example is:

x[1]*2 + x[2]*1 = 2 + 2 = 4.

etc.

The definition of a convolution includes reversing the order of one of the inputs.

Recursively filter and delete item in array of objects

I would try to build this on top of a reusable function. I abstracted a deepFilter from another answer so that we can configure the name of the child nodes (most commonly 'children', here 'layers', but I've seen many others.) The returned function takes a predicate and returns another function which takes an array, and recursively keeps only those nodes for which the predicate returns true.

Using this, we can write removeById by passing 'layers' to deepFilter along with a predicate that checks if a node's id fails to match our target. It looks like this

const deepFilter = (childProp) => (pred) => (xs) =>
xs .flatMap (({[childProp]: children = [], ...rest}) =>
pred (rest)
? [{... rest, [childProp]: deepFilter (childProp) (pred) (children)}]
: []
)

const removeById = (target) =>
deepFilter ('layers') (({id}) => id !== target)

const payload = [{id: 1, name: "Shrek", lock: false, checked: false, selected: false, layers: [/* onions have layers */]}, {id: 2, name: "Fiona", lock: false, checked: false, selected: false, layers: [{id: 4, name: "Lord Farquad", lock: false, checked: false, selected: false, layers: [{id: 5, name: "Prince Charming", lock: false, checked: false, selected: false, layers: []}]}]}, {id: 3, name: "Donkey", lock: false, checked: false, selected: false, layers: [/* parfaits have layers */]}]

console .log (
removeById (5) (payload)
)
.as-console-wrapper {max-height: 100% !important; top: 0}

Python 3.3: Recursive version of a function

Non-recursive solution:

def issorted(L):
return all(x <= y for x, y in zip(L, L[1:]))

To make a recursive function you should find a way to split the problem into smaller and/or simpler subproblems that could be solve the same way:

#!/usr/bin/env python3
from string import ascii_lowercase

def abcdearian(s):
return issorted_recursive([c for c in s.lower() if c in ascii_lowercase])

def issorted_recursive(L):
return L[0] <= L[1] and issorted_recursive(L[1:]) if len(L) > 1 else True

Here issorted_recursive() is a recursive function. The base case is len(L) <= 1 (a list with zero or one element is always sorted so return True in this case). In the recursive case (len(L) > 1) the list L is considered sorted if the first item is in the sorted order (L[0] <= L[1]) and the rest of the list (L[1:]) is also sorted. Each time the function receives smaller and smaller input until out of order element is found (L[0] > L[1]) or the base case is encountered and the function finishes.

Example

while True:
s = input("String? ")
if not s:
break
print("{} is {}abcdearian".format(s, "" if abcdearian(s) else "not "))

Input


abc
bac

Output

String? abc is abcdearian
String? bac is not abcdearian
String?

Recursive cumulative function

Since your recursion is

u[n+1] = a * ( x[n+1] + u[n] )

i.e.,

u[n+1]/a = x[n+1] + a * u[n]/a,

you can use filter:

x <- 1:5
a <- 2
a*filter(1:5, a, method="recursive")

# Compare with the expected values
a*x[1]
a*x[2] + a^2*x[1]
a*x[3] + a^2*x[2] + a^3*x[1]
a*x[4] + a^2*x[3] + a^3*x[2] + a^4*x[1]

Haskell implement a filter list with fold function

There are both problems with the filterList and filter1 function. The filterList function has as pattern:

filterList a ((x, y): xs) = …

but that does not make much sense, the type signature, and type inference will guarantee that it is a list of 2-tuples. Your pattern here will not work for an empty list, but filtering an empty list is likely still necessary. You thus should simplify it to:

filterList a ls = …

the foldr :: (a -> b -> b) -> b -> [a] -> b function is given three parameters:

  • the fold function
  • the "base-case", which is used when folding an empty list; and
  • a list of elements.

But you can not use a as base case, since that base case also determines the type of the result. The base case is here the empty list. We also need to pass a to the filter1 function, so we can implement this as:

filterList :: Eq a => a -> [(a, b)] -> [(a, b)]
filterList a ls = foldr (filter1 a) [] ls

Your filter1 function works on a list, but that is not how a foldr function works. The function you pass to foldr will be given an element, and the result of folding the rest of the list. Your function thus looks like:

filter1 :: Eq a => a -> (a, b) -> [(a, b)] -> [(a, b)]
filter1 a (x, y) rs = …

Here a is the element we have passed we have to look for, (x, y) is the 2-tuple we are "folding in", and rs is the result of folding the rest of the list. So this is a list that is already filtered.

I leave implementing filter1 as an exercise.



Related Topics



Leave a reply



Submit