mirror of
https://github.com/thma/LtuPatternFactory.git
synced 2025-01-05 19:04:46 +03:00
correction
This commit is contained in:
parent
36ff7c6eb3
commit
14a2b492a8
55
README.md
55
README.md
@ -660,39 +660,17 @@ http://blog.ploeh.dk/2018/06/25/visitor-as-a-sum-type/
|
||||
> [...] the iter pattern is a design pattern in which an iter is used to traverse a container and access the container's elements. The iter pattern decouples algorithms from containers; in some cases, algorithms are necessarily container-specific and thus cannot be decoupled.
|
||||
> [Quoted from Wikipedia] (https://en.wikipedia.org/wiki/Iterator_pattern)
|
||||
|
||||
### Iterating over a Tree
|
||||
|
||||
|
||||
```java
|
||||
private static int[] wordCountIterator(String str) {
|
||||
int nl=0, nw=0, nc=0;
|
||||
boolean readingWord = false;
|
||||
for (Iterator<Character> iter = getIteratorFor(str); iter.hasNext();) {
|
||||
Character c = iter.next();
|
||||
nc++;
|
||||
if (c == '\n') {
|
||||
nl++;
|
||||
}
|
||||
if (c == ' ' || c == '\n' || c == '\t') {
|
||||
readingWord = false;
|
||||
} else if (readingWord == false) {
|
||||
readingWord = true;
|
||||
nw++;
|
||||
}
|
||||
}
|
||||
return new int[]{nl,nw,nc};
|
||||
}
|
||||
|
||||
private static Iterator<Character> getIteratorFor(String str) {
|
||||
return str.chars().mapToObj(c -> (char) c).collect(Collectors.toList()).iterator();
|
||||
}
|
||||
```
|
||||
### Combining traversal operations
|
||||
|
||||
```java
|
||||
private static int[] wordCount(String str) {
|
||||
int nl=0, nw=0, nc=0;
|
||||
boolean readingWord = false;
|
||||
char[] chars = str.toCharArray();
|
||||
for (char c : str.toCharArray()) {
|
||||
for (Character c : asList(str)) {
|
||||
nc++;
|
||||
if (c == '\n') {
|
||||
nl++;
|
||||
@ -706,8 +684,35 @@ private static int[] wordCount(String str) {
|
||||
}
|
||||
return new int[]{nl,nw,nc};
|
||||
}
|
||||
|
||||
private static List<Character> asList(String str) {
|
||||
return str.chars().mapToObj(c -> (char) c).collect(Collectors.toList());
|
||||
}
|
||||
```
|
||||
|
||||
```java
|
||||
private static int[] wordCountIterator(String str) {
|
||||
int nl=0, nw=0, nc=0;
|
||||
boolean readingWord = false;
|
||||
for (Iterator<Character> iter = asList(str).iterator(); iter.hasNext();) {
|
||||
Character c = iter.next();
|
||||
nc++;
|
||||
if (c == '\n') {
|
||||
nl++;
|
||||
}
|
||||
if (c == ' ' || c == '\n' || c == '\t') {
|
||||
readingWord = false;
|
||||
} else if (readingWord == false) {
|
||||
readingWord = true;
|
||||
nw++;
|
||||
}
|
||||
}
|
||||
return new int[]{nl,nw,nc};
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
## Typeclasses Category, Arrow & Co.
|
||||
Theses typeclasses aim at generalizing elements of Monads or Functors.
|
||||
|
@ -5,24 +5,20 @@ import Data.Functor.Const
|
||||
import Data.Monoid (Sum (..), getSum)
|
||||
|
||||
instance Functor Exp where
|
||||
fmap f (Var x) = Var x
|
||||
fmap f (Val a) = Val $ f a
|
||||
fmap f (Add x y) = Add (fmap f x) (fmap f y)
|
||||
fmap f (Mul x y) = Mul (fmap f x) (fmap f y)
|
||||
fmap f (Var x) = Var x
|
||||
fmap f (Val a) = Val $ f a
|
||||
fmap f (Add x y) = Add (fmap f x) (fmap f y)
|
||||
fmap f (Mul x y) = Mul (fmap f x) (fmap f y)
|
||||
|
||||
instance Traversable Exp where
|
||||
traverse g (Var x) = pure $ Var x
|
||||
traverse g (Val x) = Val <$> g x
|
||||
traverse g (Add l r) = Add <$> traverse g l
|
||||
<*> traverse g r
|
||||
traverse g (Mul l r) = Mul <$> traverse g l
|
||||
<*> traverse g r
|
||||
traverse g (Add x y) = Add <$> traverse g x <*> traverse g y
|
||||
traverse g (Mul x y) = Mul <$> traverse g x <*> traverse g y
|
||||
|
||||
str :: [Char]
|
||||
str = "hello \n nice \t and \n busy world"
|
||||
|
||||
|
||||
|
||||
type Count = Const (Sum Integer)
|
||||
count :: a -> Count b
|
||||
count _ = Const 1
|
||||
@ -61,6 +57,6 @@ iteratorDemo = do
|
||||
let exp = Mul (Add (Val 3) (Val 1))
|
||||
(Mul (Val 2) (Var "pi"))
|
||||
env = [("pi", pi)]
|
||||
print $ traverse (\x -> if even x then [x] else [2*x]) exp
|
||||
print $ traverse (\x c -> if even x then [x] else [2*x]) exp 0
|
||||
--print $ traverse count str
|
||||
|
Loading…
Reference in New Issue
Block a user