Redian新闻
>
Scala的map和flatmap什么区别?
avatar
Scala的map和flatmap什么区别?# Programming - 葵花宝典
f*8
1
这个法案是CT的参议员Lieberman提出的,这个法案总共三个部分:第一和第三部分都
不是关于绿卡,而是关于工作签证的,第二部分是关于EB类的绿卡,不过这个法案并没
有增加EB类的绿卡名额,而只是允许提前交I-485(交500美元处理费)和签发有效期为三
年的EAD和AP. 这实际上是个很moderate的改革,可惜当时没有通过。现在把法案贴出
来, 我觉着今年有可能会有议员提相类似的法案:
S.1397 -- Skilled Worker Immigration and Fairness Act (Introduced in Senate
- IS)
S 1397 IS
110th CONGRESS
1st Session
S. 1397
To increase the allocation of visas for certain highly skilled workers and
to reduce fraud and abuse in certain visa programs for aliens working
temporarily in the United States.
IN THE SENATE OF THE UNITED STATES
May 15, 2007
Mr. LIEBERMAN (for himself, Mr. HAGEL, Ms. CANTWELL, and Mr. VOINOVICH)
introduced the following bill; which was read twice and referred to the
Committee on the Judiciary
A BILL
To increase the allocation of visas for certain highly skilled workers and
to reduce fraud and abuse in certain visa programs for aliens working
temporarily in the United States.
Be it enacted by the Senate and House of Representatives of the United
States of America in Congress assembled,
SECTION 1. SHORT TITLE; TABLE OF CONTENTS.
(a) Short Title- This Act may be cited as the `Skilled Worker Immigration
and Fairness Act' .
(b) Table of Contents- The table of contents for this Act is as follows:
Sec. 1. Short title; table of contents.
Sec. 2. H-1B visas.
Sec. 3. Employment-based immigration .
Sec. 4. H-1B visa fraud and abuse protections.
SEC. 2. H-1B VISAS.
(a) Exemptions to Numerical Limitations-
(1) IN GENERAL- Section 214(g)(5) of the Immigration and Nationality Act (8
U.S.C. 1184(g)(5)) is amended--
(A) in subparagraph (C), by striking `until the number of aliens who are
exempted from such numerical limitation during such year exceeds 20,000.'
and inserting `or has been awarded a medical specialty certification based
on post-doctoral training and experience in the United States; or'; and
(B) by adding at the end the following:
`(D) has earned a masters or higher degree in science, technology,
engineering, or mathematics from an institution of higher education outside
of the United States.'.
(2) APPLICABILITY- The amendments made by paragraph (1) shall apply to--
(A) any petition or visa application pending on the date of the enactment of
this Act ; and
(B) any petition or visa application filed on or after such date.
(b) Market-Based Visa Limits- Section 214(g) of such Act (8 U.S.C. 1184(g)),
as amended by subsection (a), is further amended--
(1) in paragraph (1)--
(A) in the matter preceding subparagraph (A), by striking `(beginning with
fiscal year 1992)'; and
(B) in subparagraph (A), by striking clauses (i) through (vii) and inserting
the following:
`(i) 115,000 in fiscal year 2007; and
`(ii) in fiscal year 2008, and in each subsequent fiscal year, the greater
of--
`(I) 115,000; or
`(II) the number calculated under paragraph (9);';
(2) in paragraph (8)--
(A) in subparagraph (B), by striking clause (iv); and
(B) by striking subparagraph (D);
(3) by redesignating paragraphs (9), (10), and (11) as paragraphs (10), (11)
, and (12), respectively; and
(4) by inserting after paragraph (8) the following:
`(9) If the numerical limitation under paragraph (1)(A)--
`(A) is reached during a given fiscal year, the numerical limitation under
paragraph (1)(A) for the subsequent fiscal year shall be equal to the lesser
of--
`(i) 120 percent of the numerical limitation for the given fiscal year; or
`(ii) 180,000; and
`(B) is not reached during a given fiscal year, the numerical limitation
under paragraph (1)(A) for the subsequent fiscal year shall be equal to the
numerical limitation for the given fiscal year.'.
SEC. 3. EMPLOYMENT-BASED IMMIGRATION .
(a) In General- Section 201(b)(1) of the Immigration and Nationality Act (8
U.S.C. 1151(b)(1)) is amended by adding at the end the following:
`(F) Aliens who have earned a master's or higher degree from an accredited
university in the United States.
`(G) Aliens who--
`(i) have earned an advanced degree in science, technology, engineering, or
mathematics; and
`(ii) have been working in a related field in the United States under a
nonimmigrant visa during the 3-year period preceding their application for
an immigrant visa under section 203(b).
`(H) Aliens who--
`(i) are described in subparagraph (A) or (B) of section 203(b)(1); or
`(ii) have received a national interest waiver under section 203(b)(2)(B).
`(I) The immediate relatives of an alien who is admitted as an employment-
based immigrant under section 203(b).'.
(b) Adjustment of Status for Employment-Based Immigrants-
(1) IN GENERAL- Section 245 of the Immigration and Nationality Act (8 U.S.C.
1255) is amended by adding at the end the following:
`(n) Adjustment of Status to Employment-Based Immigrant-
`(1) ELIGIBILITY- An alien, and any eligible dependents of such alien, may
file an application for adjustment of status with the Secretary of Homeland
Security, whether or not an employment-based immigrant visa is immediately
available at the time the application is filed, if--
`(A) a petition filed under subparagraph (E) or (F) of section 204(a)(1) on
behalf of the alien has been approved; or
`(B) in the discretion of the Secretary, the adjudication of such petition
is pending.
`(2) VISA AVAILABILITY- An application filed under paragraph (1) may not be
approved until the appropriate employment-based immigrant visa becomes
available under section 203(b).
`(3) FEES- If an employment-based immigrant visa is not available on the
date on which an application is filed under paragraph (1), a supplemental
fee of $500 shall be paid on behalf of the beneficiary of such application.
Such fee may not be charged with respect to any dependent accompanying or
following to join such beneficiary.
`(o) Extension of Employment Authorization and Advanced Parole Document- The
Secretary of Homeland Security--
`(1) shall issue a 3-year employment authorization and 3-year advanced
parole document to any beneficiary of an application for adjustment of
status if a petition has been filed or is pending under subparagraph (E) or
(F) of section 204(a)(1); and
`(2) may adjust fees assessed under this section in accordance to the 3-year
period of validity assigned to the employment authorization or advanced
parole documents issued under subparagraph (1).'.
(2) USE OF FEES- Section 286 of such Act (8 U.S.C. 1356) is amended--
(A) in subsection (m), by striking `provisions of law, all adjudication fees
' and inserting `provision of law, all adjudication fees and the fees
collected under section 245(n)(3)'; and
(B) in subsection (n)--
(i) by striking `All deposits' and inserting the following: `(1) Except as
provided in paragraph (2), all deposits'; and
(ii) by adding at the end the following:
`(2) All deposits in the Immigration Examinations Fee Account that were
originally collected under section 245(n)(3) shall be used to clear security
background check delays.'.
(c) Applicability- The amendments made by subsections (a) and (b) shall
apply to any visa application--
(1) pending on the date of the enactment of this Act ; or
(2) filed on or after such date.
SEC. 4. H-1B VISA FRAUD AND ABUSE PROTECTIONS.
(a) Prohibition Against Advertising Exclusively to H-1B Nonimmigrants-
Section 212(n)(1) of the Immigration and Nationality Act (8 U.S.C. 1182(n)(1
)) is amended--
(1) by redesignating subparagraph (G) as subparagraph (H);
(2) by inserting after subparagraph (H), as redesignated, the following:
`(I) The employer has not advertised the available jobs specified in the
application in an advertisement that states or indicates that--
`(i) the jobs are only available to persons who are, or may become, H-1B
nonimmigrants; or
`(ii) persons will receive priority or preference in the hiring process
because they are, or may become, H-1B nonimmigrants.'; and
(3) in the undesignated paragraph at the end, by striking `The employer' and
inserting the following:
`(K) The employer'.
(b) Limit on Percentage of H-1B Employees- Section 212(n)(1) of such Act ,
as amended by this section, is further amended by inserting after
subparagraph (I), as added by subsection (a)(1), the following:
`(J) If the employer employs 50 or more employees in the United States, not
more than 50 percent of such employees are H-1B nonimmigrants.'.
(c) Safeguards Against Fraud and Misrepresentation in Application Review
Process- Section 212(n)(1)(K) of such Act , as designated by subsection (a)(
2), is amended--
(1) by inserting `, clear indicators of fraud, misrepresentation of material
fact,' after `completeness';
(2) by striking `or obviously inaccurate' and inserting `, presents clear
indicators of fraud or misrepresentation of material fact, or is obviously
inaccurate'; and
(3) by adding at the end the following: `If the Secretary's review of an
application identifies clear indicators of fraud or misrepresentation of
material fact, the Secretary may conduct an investigation and hearing under
paragraph (2).'.
(d) Investigations by Department of Labor- Section 212(n)(2) of such Act is
amended--
(1) in subparagraph (A), by striking `12 months' and all that follows and
inserting `24 months after the date of the failure or misrepresentation,
respectively. Upon the receipt of such a complaint, the Secretary may
initiate an investigation to determine if such a failure or
misrepresentation has occurred.';
(2) in subparagraph (C)(i)--
(A) by striking `a condition of paragraph (1)(B), (1)(E), or (1)(F)' and
inserting `a condition under subparagraph (B), (C), (E), (F), (H), (I), or (
J) of paragraph (1)'; and
(B) by striking `paragraph (1)(C), (1)(D), or (1)(G)(i)(I)' and inserting `
subparagraph (C), (D) or (G)(i)(I) of paragraph (1)';
(3) in subparagraph (G)--
(A) in clause (i), by striking `if the Secretary' and all that follows and
inserting `with regard to the employer's compliance with the requirements
under this subsection.';
(B) in clause (ii), by striking `and whose identity' and all that follows
through `failure or failures.' and inserting `the Secretary of Labor may
conduct an investigation into the employer's compliance with the
requirements under this subsection.';
(C) in clause (iii), by striking the last sentence;
(D) by striking clauses (iv) and (v);
(E) by redesignating clauses (vi), (vii), and (viii) as clauses (iv), (v),
and (vi), respectively;
(F) in clause (iv), as redesignated, by striking `meet a condition' and all
that follows and inserting `comply with the requirements under this
subsection, unless the Secretary of Labor receives the information not later
than 24 months after the date of the alleged failure.';
(G) by amending clause (v), as redesignated, to read as follows:
`(v) The Secretary of Labor shall provide notice to an employer of the
intent to conduct an investigation. The notice shall be provided in such a
manner, and shall contain sufficient detail, to permit the employer to
respond to the allegations before an investigation is commenced. The
Secretary is not required to comply with this clause if the Secretary
determines that such compliance would interfere with an effort by the
Secretary to investigate the employer or secure the employer's compliance
with this subsection. A determination by the Secretary under this clause is
not subject to judicial review.';
(H) in clause (vi), as redesignated, by striking `An investigation' and all
that follows through `the determination.' and inserting `If the Secretary of
Labor, after an investigation under clause (i) or (ii), determines that a
reasonable basis exists to make a finding that the employer has failed to
comply with the requirements under this subsection, the Secretary shall
provide interested parties with notice of such determination and an
opportunity for a hearing in accordance with section 556 of title 5, United
States Code, not later than 120 days after the date of such determination.';
and
(I) by adding at the end the following:
`(vii) If the Secretary of Labor, after a hearing, finds a reasonable basis
to believe that the employer has violated a requirement under this
subsection, the Secretary may impose a penalty under subparagraph (C).';
(4) by redesignating subparagraph (I) as subparagraph (J).
(e) Additional Department of Labor Employees-
(1) IN GENERAL- The Secretary of Labor is authorized to hire 200 additional
employees to administer, oversee, investigate, and enforce programs
involving H-1B nonimmigrant workers.
(2) AUTHORIZATION OF APPROPRIATIONS- There are authorized to be appropriated
such sums as may be necessary to carry out this subsection.
(f) Schedule of Fees- Section 214(c)(12)(C) of the Immigration and
Nationality Act (8 U.S.C. 1184(c)(12)(C)) is amended by striking `$500' and
inserting `$1,000'.
(g) Information Sharing Between Department of Labor and Department of
Homeland Security- Section 212(n)(2) of such Act , as amended by this
section, is further amended by inserting after subparagraph (H) the
following:
`(I) If any information contained in the materials submitted by employers of
H-1B nonimmigrants as part of the adjudication process indicates that the
employer is not complying with the requirements under this subsection, the
Director of United States Citizenship and Immigration Services shall provide
such information to the Secretary of Labor. The Secretary may initiate and
conduct an investigation and hearing under this paragraph after receiving
such information.'.
(h) Audits- Section 212(n)(2)(A) of such Act , as amended by this section,
is further amended by adding at the end the following: `The Secretary may
conduct surveys regarding the degree to which employers comply with the
requirements under this subsection and may conduct annual compliance audits
of employers of H-1B nonimmigrants. The Secretary shall conduct annual
compliance audits of not less than 1 percent of the employers of H-1B
nonimmigrants during the applicable calendar year. The Secretary shall
conduct annual compliance audits of each employer with more than 100
employees who work in the United States if more than 15 percent of such
employees are H-1B nonimmigrants.'.
(i) Penalties- Section 212(n)(2)(C) of such Act , as amended by this section
, is further amended--
(1) in clause (i)(I), by striking `$1,000' and inserting `$2,000';
(2) in clause (ii)(I), by striking `$5,000' and inserting `$10,000'; and
(3) in clause (vi)(III), by striking `$1,000' and inserting `$2,000'.
(j) Information Provided to H-1B Nonimmigrants Upon Visa Issuance- Section
212(n) of such Act , as amended by this section, is further amended by
adding at the end the following:
`(6)(A) Upon providing H-1B nonimmigrant status to an alien in the United
States, the office processing the petition for such status shall provide the
applicant with--
`(i) a brochure outlining the employer's obligations and the employee's
rights under Federal law, including labor and wage protections; and
`(ii) the contact information for Federal agencies that can offer more
information or assistance in clarifying employer obligations and workers'
rights.
`(B) Upon issuing an H-1B nonimmigrant visa to an alien outside the United
States, the officer of the Department of State shall provide the applicant
with the items described in clauses (i) and (ii) of subparagraph (A).'.
avatar
l*n
2
我有个Option[T], 用map和flatmap有什么区别?返回什么type?
avatar
p*e
3
现在没有办法,合法移民给非法移民绑架了,都说了:单独合法移民的法案不会得到支
持非法移民的议员支持。必须捆绑
avatar
l*n
4
用map是返回一个Option[_]吧?如果f: T => V, 就用map,返回Option[V], 如果f: T
=> Option[V],就用flatmap, 返回Option[V], am I right?
avatar
f*8
5
七月中旬众议院的Judiciary committee本来要举行一个high skilled immigration
act的听证,可惜临时给取消了,当时要听证的那个high skilled immigration的act名
字和上述法案非常类似:
Fairness for High Skilled Immigrants Act.
希望下面众议院接着把这个议案的听证做完。这个议案到现在还看不到全文,可能还没
有提出。
avatar
p*2
6
functor monad 的区别

【在 l**********n 的大作中提到】
: 我有个Option[T], 用map和flatmap有什么区别?返回什么type?
avatar
f*8
7
情况可能也有所改变。
你看今年罗富贵提出的那个IDEA Act. 该法案的co-sponsor里面包括一直要非法移民大
赦的Representative Luis Gutierrez. 这个议员可是非法移民最坚定的支持者,而他
这一次竟然也联署了罗富贵这个高科技合法移民的法案。

【在 p***e 的大作中提到】
: 现在没有办法,合法移民给非法移民绑架了,都说了:单独合法移民的法案不会得到支
: 持非法移民的议员支持。必须捆绑

avatar
g*g
8
map是一对一的,flatmap可以是一对N的。典型的例子就是value是个list。map返回
list of list,flatmap返回merged的list.

【在 l**********n 的大作中提到】
: 我有个Option[T], 用map和flatmap有什么区别?返回什么type?
avatar
j*e
9
"`(1) ELIGIBILITY- An alien, and any eligible dependents of such alien, may
file an application for adjustment of status with the Secretary of Homeland
Security, WHETHER OR NOT AN EMPLOYMENT-BASED IMMIGRANT VISA IS IMMEDIATELY AVAILABLE at the time the application is filed"
这是真正的I-485递交与批准结构性分离。呵呵。
avatar
p*2
10

list只是一种monad

【在 g*****g 的大作中提到】
: map是一对一的,flatmap可以是一对N的。典型的例子就是value是个list。map返回
: list of list,flatmap返回merged的list.

avatar
R*n
11
收藏

Senate

【在 f*******8 的大作中提到】
: 这个法案是CT的参议员Lieberman提出的,这个法案总共三个部分:第一和第三部分都
: 不是关于绿卡,而是关于工作签证的,第二部分是关于EB类的绿卡,不过这个法案并没
: 有增加EB类的绿卡名额,而只是允许提前交I-485(交500美元处理费)和签发有效期为三
: 年的EAD和AP. 这实际上是个很moderate的改革,可惜当时没有通过。现在把法案贴出
: 来, 我觉着今年有可能会有议员提相类似的法案:
: S.1397 -- Skilled Worker Immigration and Fairness Act (Introduced in Senate
: - IS)
: S 1397 IS
: 110th CONGRESS
: 1st Session

avatar
g*g
12
我觉得你给这些高大上的词无助于解释他们的区别。

【在 p*****2 的大作中提到】
:
: list只是一种monad

avatar
p*2
13

我一直以为LZ是懂monad的。

【在 g*****g 的大作中提到】
: 我觉得你给这些高大上的词无助于解释他们的区别。
avatar
l*n
14
Option是monad吧?Scala的monad是通过trait实现的吧?

【在 p*****2 的大作中提到】
:
: 我一直以为LZ是懂monad的。

avatar
l*n
15
apply method is the unit function

【在 l**********n 的大作中提到】
: Option是monad吧?Scala的monad是通过trait实现的吧?
avatar
l*n
16
because scala is oop, it is mentally hard thinking in fp. for example,
flatmap is a trait of monad, but map is a trait of functor

【在 l**********n 的大作中提到】
: apply method is the unit function
avatar
l*n
17
scala use trait and implicit to provide type class and type class members.

【在 l**********n 的大作中提到】
: because scala is oop, it is mentally hard thinking in fp. for example,
: flatmap is a trait of monad, but map is a trait of functor

avatar
d*e
18
flatmap就是map后再flat一下

【在 l**********n 的大作中提到】
: 我有个Option[T], 用map和flatmap有什么区别?返回什么type?
avatar
H*S
19
返回的类型都是一样的,但是一个接受A => B,另外一个接受A => Option[B]。实际上
map是多余的,
Option.map(f) = Option.flatMap(f andThen Option.apply)

【在 l**********n 的大作中提到】
: 我有个Option[T], 用map和flatmap有什么区别?返回什么type?
avatar
c*o
20
There are things that has map but does NOT have flatMap

【在 H****S 的大作中提到】
: 返回的类型都是一样的,但是一个接受A => B,另外一个接受A => Option[B]。实际上
: map是多余的,
: Option.map(f) = Option.flatMap(f andThen Option.apply)

avatar
d*r
21
这个只对list成立。真正的理解从monad开始。

【在 g*****g 的大作中提到】
: map是一对一的,flatmap可以是一对N的。典型的例子就是value是个list。map返回
: list of list,flatmap返回merged的list.

avatar
H*S
22
他是问option的情况,我这样回答有什么错?我觉得好虫说得对,不要一上来就扯
monad和functor,要针对具体问题具体回答。

【在 c******o 的大作中提到】
: There are things that has map but does NOT have flatMap
avatar
p*2
23
为什么是多余的

【在 H****S 的大作中提到】
: 他是问option的情况,我这样回答有什么错?我觉得好虫说得对,不要一上来就扯
: monad和functor,要针对具体问题具体回答。

avatar
z*e
24
scala的这部分解释起来费劲
看rxjava就很清晰了
map( ) — transform the items emitted by an Observable by applying a
function to each of them
flatMap( ), concatMap( ), and flatMapIterable( ) —
transform the items emitted by an Observable into Observables (or Iterables)
, then flatten this into a single Observable
map: 1 -> 1
flatmap: 1 -> *或者说[0,n]
avatar
p*2
25
解释了个表面

Iterables)

【在 z****e 的大作中提到】
: scala的这部分解释起来费劲
: 看rxjava就很清晰了
: map( ) — transform the items emitted by an Observable by applying a
: function to each of them
: flatMap( ), concatMap( ), and flatMapIterable( ) —
: transform the items emitted by an Observable into Observables (or Iterables)
: , then flatten this into a single Observable
: map: 1 -> 1
: flatmap: 1 -> *或者说[0,n]

avatar
q*c
26
但是有助于逼格啊。

【在 g*****g 的大作中提到】
: 我觉得你给这些高大上的词无助于解释他们的区别。
avatar
c*o
27
从下面的code可以看出,flatMap 可以实现map,所以flatMap更强大(比map能干的事多
), 但是map更常见 (有map不一定能实现flatMap)
// id function:
// def id[A](a: A): A = a
// compose function:
// def compose[A,B,C](f: B => C, g: A => B): A => C =
// a => f(g(a))
trait Functor[F[_]] {
def map[A,B](fa: F[A])(f: A => B): F[B]
}
// Functor Law
// identity: map(x)(id) == x
// composition: map(a)(compose(f, g)) == map(map(a,g), f)
trait Applictive[F[_]] extends Functor[F] {
def unit[A](a: => A): F[A]
def ap[A,B](la: F[A])(f: F[A => B]): F[B]
override def map[A, B](la: F[A])(f: A => B): F[B] =
ap(la)(unit(f))
}
// Applicative Law
// identity: ap(a, unit(id)) == a
// composition: ap(ap(a, g), f) == ap(a, ap(g, ap(f, unit(compose))))
// homomorphism: ap(unit(a), unit(f)) == unit(f(a))
// interchange: ap(unit(a), f) == ap(f, unit(f => f(x)))
trait Monad[F[_]] extends Applictive[F] {
def unit[A](a: => A): F[A]
def flatMap[A,B](ma: F[A])(f: A => F[B]): F[B]
override def ap[A,B](la: F[A])(f: F[A => B]): F[B] =
flatMap(f)(t1 => flatMap(la)(t2 => unit(t1(t2))))
override def map[A,B](ma: F[A])(f: A => B): F[B] =
flatMap(ma)(a => unit(f(a)))
}
// Monad Law
// left identity: f(a) == flatmap(unit(a), f)
// right identity: a == flatMap(a, x => unit(x))
// associativity: flatMap(a, x => flatMap(f(x), g)) == flatMap(flatMap(a, f)
, g)
avatar
c*o
28
从下面的code 看出。
map能对一个数据(一个List, 一个Option, 一个Future) 进行操作。
applicative (ap) 能对多个数据进行操作。
monad能对对多个数据进行操作的同时,根据数据的内容动态改变操作流程。
基本上functor (map),applicative functor (ap), monad (flatMap)的区别就是这
些。
object test {
def oneVarFunc: Int => Int = {
_ + 1
}
def twoVarFunc: (Int, Int) => Int = {
_ + _
}
def optionap[A,B](a: Option[A])(f: Option[A => B]): Option[B] =
f.flatMap(t1 => a.flatMap(t2 => Some(t1(t2))))
val x1 = Some(1)
val x2 = Some(2)
val x3 = None
//functor
def test1 =
x1.map(oneVarFunc)
//Applicative Functor
def test2 = {
optionap(x2)(x1.map(twoVarFunc.curried))
}
def test2_1 = {
optionap(x3)(x2.map(twoVarFunc.curried))
}
//Monad
def test3 = {
for {
r1 r2 } yield {
if(r1 == 1) {
r1 * r2
} else {
r1 + r2
}
}
}
def compose[A,B,C](f: A => List[B], g: B => List[C]): A => List[C] = ???
}
avatar
p*2
29
monad 一定是 functor
functor 不一定是monad
所以map不是可有可无的

【在 c******o 的大作中提到】
: 从下面的code可以看出,flatMap 可以实现map,所以flatMap更强大(比map能干的事多
: ), 但是map更常见 (有map不一定能实现flatMap)
: // id function:
: // def id[A](a: A): A = a
: // compose function:
: // def compose[A,B,C](f: B => C, g: A => B): A => C =
: // a => f(g(a))
: trait Functor[F[_]] {
: def map[A,B](fa: F[A])(f: A => B): F[B]
: }

avatar
d*e
30
还在讨论。俺们草蜢快类型的根本不管什么术语。上个 code就明白了。以future为例:
def temperatureOkay(water: Water): Future[Boolean] = Future {
(80 to 85).contains(water.temperature)
}
val nestedFuture: Future[Future[Boolean]] = heatWater(Water(25)).map {
water => temperatureOkay(water)
}
val flatFuture: Future[Boolean] = heatWater(Water(25)).flatMap {
water => temperatureOkay(water)
}
val acceptable: Future[Boolean] = for {
heatedWater okay } yield okay
网上的例子。简单说,为了box类型 (monad)的composable,又不想option option
option 1000 times [A]
就用flat map,或者for compreshion。其实还是for -comprehenision好懂,不用大讨
论了。

【在 p*****2 的大作中提到】
: monad 一定是 functor
: functor 不一定是monad
: 所以map不是可有可无的

avatar
p*2
31
future是monad呀
你要用list map更普遍 更容易懂

例:

【在 d******e 的大作中提到】
: 还在讨论。俺们草蜢快类型的根本不管什么术语。上个 code就明白了。以future为例:
: def temperatureOkay(water: Water): Future[Boolean] = Future {
: (80 to 85).contains(water.temperature)
: }
: val nestedFuture: Future[Future[Boolean]] = heatWater(Water(25)).map {
: water => temperatureOkay(water)
: }
: val flatFuture: Future[Boolean] = heatWater(Water(25)).flatMap {
: water => temperatureOkay(water)
: }

avatar
d*e
32
说list大家都明白。换到option上就不太好理解了。其实就是壳子套壳子,再打开一个
壳子。monad这写名词我学玩haskell早忘光了。又不每天用

【在 p*****2 的大作中提到】
: future是monad呀
: 你要用list map更普遍 更容易懂
:
: 例:

avatar
l*n
33
it is about non-deterministic. so Option is a good example. try is good too.
container is not a good explanation. container is about type safety.

【在 d******e 的大作中提到】
: 说list大家都明白。换到option上就不太好理解了。其实就是壳子套壳子,再打开一个
: 壳子。monad这写名词我学玩haskell早忘光了。又不每天用

avatar
d*e
34
不是很确定我理解正确与否。特别是type safety.
不过box或者container是通用的比喻。
I already mentioned that Option[A] is a container for a value of type A.
More precisely, you may think of it as some kind of collection – some
special snowflake of a collection that contains either zero elements or
exactly one element of type A. This is a very powerful idea!
还有:
http://learnyouahaskell.com/functors-applicative-functors-and-m

too.

【在 l**********n 的大作中提到】
: it is about non-deterministic. so Option is a good example. try is good too.
: container is not a good explanation. container is about type safety.

avatar
l*n
35
你要说collection, 熟悉oop的人就会以为是collection API, 其实更应该是个type
class.

【在 d******e 的大作中提到】
: 不是很确定我理解正确与否。特别是type safety.
: 不过box或者container是通用的比喻。
: I already mentioned that Option[A] is a container for a value of type A.
: More precisely, you may think of it as some kind of collection – some
: special snowflake of a collection that contains either zero elements or
: exactly one element of type A. This is a very powerful idea!
: 还有:
: http://learnyouahaskell.com/functors-applicative-functors-and-m
:
: too.

avatar
p*2
36

那是因为flapMap这个函数名起的有问题,换成bind就明了了。

【在 d******e 的大作中提到】
: 说list大家都明白。换到option上就不太好理解了。其实就是壳子套壳子,再打开一个
: 壳子。monad这写名词我学玩haskell早忘光了。又不每天用

avatar
z*e
37

no
collection和collections是两回事
java中也是这样的

【在 l**********n 的大作中提到】
: 你要说collection, 熟悉oop的人就会以为是collection API, 其实更应该是个type
: class.

avatar
z*e
38
换bind才崩溃
bind另有他用
在observer时候用得最多

【在 p*****2 的大作中提到】
:
: 那是因为flapMap这个函数名起的有问题,换成bind就明了了。

avatar
z*e
39
没几个人用术语,整个回帖看下来
多数人对于术语的使用是一种批判的态度
更多人是举例,解决问题

例:

【在 d******e 的大作中提到】
: 还在讨论。俺们草蜢快类型的根本不管什么术语。上个 code就明白了。以future为例:
: def temperatureOkay(water: Water): Future[Boolean] = Future {
: (80 to 85).contains(water.temperature)
: }
: val nestedFuture: Future[Future[Boolean]] = heatWater(Water(25)).map {
: water => temperatureOkay(water)
: }
: val flatFuture: Future[Boolean] = heatWater(Water(25)).flatMap {
: water => temperatureOkay(water)
: }

avatar
l*n
40
一堆人都没有真正理解scala, 包招那些monad挂在嘴边的。this is because there is
an implicit conversion to Iterable, so that one can call Iterable methods
on Option.

【在 z****e 的大作中提到】
: 没几个人用术语,整个回帖看下来
: 多数人对于术语的使用是一种批判的态度
: 更多人是举例,解决问题
:
: 例:

avatar
p*2
41

is
it's implementation details if it's true. concept is at higher level. To
understand monad, you don't have to know implementation.

【在 l**********n 的大作中提到】
: 一堆人都没有真正理解scala, 包招那些monad挂在嘴边的。this is because there is
: an implicit conversion to Iterable, so that one can call Iterable methods
: on Option.

avatar
l*t
42

我喜欢用context。context处理context内部的事,你处理你的事。

【在 d******e 的大作中提到】
: 不是很确定我理解正确与否。特别是type safety.
: 不过box或者container是通用的比喻。
: I already mentioned that Option[A] is a container for a value of type A.
: More precisely, you may think of it as some kind of collection – some
: special snowflake of a collection that contains either zero elements or
: exactly one element of type A. This is a very powerful idea!
: 还有:
: http://learnyouahaskell.com/functors-applicative-functors-and-m
:
: too.

avatar
p*2
43
container with context

【在 l******t 的大作中提到】
:
: 我喜欢用context。context处理context内部的事,你处理你的事。

avatar
l*t
44

也可以这么说。box也没问题。自己手办几个就明白了。

【在 p*****2 的大作中提到】
: container with context
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。