나는 예를 들어, 단지 얼랑을 배우기 시작, 정말 자신의 지능형리스트 구문 같은 :얼랑 루비 대 지능형리스트
이 경우Weather = [{toronto, rain}, {montreal, storms}, {london, fog}, {paris, sun}, {boston, fog}, {vancounver, snow}].
FoggyPlaces = [X || {X, fog} <- Weather].
, FoggyPlaces
이 "런던"와 "보스턴"평가합니다.
Ruby에서 가장 좋은 방법은 무엇입니까? 예를 들어
, (나는 매우 일반적인 생각)와 같은 배열 :
weather = [{city: 'toronto', weather: :rain}, {city: 'montreal', weather: :storms}, {city: 'london', weather: :fog}, {city: 'paris', weather: :sun}, {city: 'boston', weather: :fog}, {city: 'vancounver', weather: :snow}]
지금은 '때까지 가지고 최선은 다음과 같습니다
weather.collect {|w| w[:city] if w[:weather] == :fog }.compact
그러나이 경우
, 내가 전화를해야compact
을 사용하여
nil
값을 제거하십시오. 예제 자체는 Erlang만큼 읽을 수 없습니다.
얼랭 예제에서 city
과 weather
은 모두 원자입니다. 나는 루비에서 뭔가 의미있는 것을 얻고 이것처럼 보이는 것을 얻는 방법조차 모른다.
FAQ입니다. http://stackoverflow.com/questions/11319429/does-ruby-have-something-like-pythons-list-comprehensionsexample, http://stackoverflow.com/questions/310426/list-comprehension -in-ruby, http://stackoverflow.com/questions/9737525/list-comprehension-in-haskell-python-and-ruby. – tokland
erlang은 매우 선언적이며 상징적입니다. 인간과 매우 가깝습니다. –