Xóa bài viết
Bạn có chắc chắn muốn xóa bài viết này không ?
Xóa bình luận
Bạn có chắc chắn muốn xóa bình luận này không ?
Functional Programming Patterns
Functional Programming Patterns
- Adding a function parameter (Parameterization)
- Passing a function parameter (Currying)
-
Applying a function (
$
) -
Composing a function: applies a succession of transformations to supplied data (
(.)
) -
Folding: reduces a sequence of values to a single value by combining adjacent pairs (
foldr1
) -
Mapping: applies the same transformation to each value in a sequence (
fmap
) -
Iteration: applies the same transformation repeatedly to its own delivered values, building a sequence of successively more refined iterates (
iterate
)
iterate f x = [d0, d1, d2,..]
where [d0, d1, d2,...] = [x, f x, (f . f) x, ...]
- Zipping: delivers a sequence whose length matches that of the shorter of the sequences supplied as its last two arguments. Zipping stops when one of those arguments runs out of elements.
zipWith op [x1, x2, x3, ..] [y1, y2 , y3 , ...] =
[op x1 y1, op x2 y2 , op x3 y3 , ... ]
Example:
fibs :: [Int]
fibs = 0 : 1 : zipWith (+) fibs (tail fibs)
- Filtering: selecting certain elements from a given sequence to form a new one.
-- Filter as Folding:
filter keep = foldr op []
where
op x ys
| keep x = [x] ++ ys
| otherwise = ys
Note: When you are trying to describe a computation that involves repetition, try to view it as one of common patterns: mapping, folding, filtering, iteration, and zipping. The operators map, foldr, filter, iterate, and zipWith make up a kind of linguistic shorthand that covers probably over 90% of the computations involving repetition that you will encounter in practice.
- Polymorphic: operate on many different types of arguments (
reverse
) - Type Class: collection of types that share a collection of functions and/or operations (
Eq
), for example, withEq
class, it made comparision possible. - Higher order function: Functions that have an argument that is, itself, a function are called higher-order functions. Functions that deliver a value that is, itself, a function are also called higher-order functions. The composition operator is a higher-order function that does both.
- Encapsulation: Isolating components of software so that modifying internal details in one com- ponent will have no affect on other components of the software (
where
)
checkraiser 23-06-2018
Bình luận

{{ comment.user.name }}
Bỏ hay
Hay

Cùng một tác giả

6
1
Tôi dự sẽ viết 1 loạt series về việc thiết kế 1 ứng dụng Rails như thế nào để nó có thể giúp bạn ăn ngon ngủ yên trong hằng năm trời: Khi mà việc t...

6
10
Cũng ngót nghét đi làm hơn 6 năm rồi, mình chỉ thấy một điều khá "ngược đời": Các ông chủ , những người trả tiền cho bạn lại có tư duy lập trình ké...

5
0
Bài viết này, là 1 quan điểm cá nhân trong việc viết Code các layer trong Rails của mình. Controller Trước khi code, bạn hãy đặt câu hỏi: Mục đí...
Bài viết liên quan

41
8
Có lẽ một số bạn sẽ có thắc mắc như tiêu đề khi mới tìm hiểu về Rust. Hẳn là các bạn đã biết, hoặc nghe đâu đó là: Trong Rust không có NULL. Bài vi...

0
5
fCC: Technical Documentation Page note So I have finished the HTML part of this exercise and I want to come here to lament about the lengthy HTML ...