2011-07-04 4 views
5

에서 어떻게 완성됩니까?이 C# 코드를 F # 또는 Haskel 또는 유사한 함수 언어로 작성하려면 어떻게해야합니까? 당신이 그들을 아래의 URL을 그룹화 헤더를 원하는,이에서이 C# 코드는 함수형 언어 (F #? Haskel?)

[...] 
Foo 
https://example1.com 
https://example2.com 
Bar 
https://example3.com 
Baz 
Xyzzy 
https://example4.com 
[...] 

: 내가 제대로 코드를 읽으면

var lines = File.ReadAllLines(@"\\ad1\\Users\aanodide\Desktop\APIUserGuide.txt"); 

// XSDs are lines 375-471 
var slice = lines.Skip(374).Take(471-375+1); 

var kvp = new List<KeyValuePair<string, List<string>>>(); 
slice.Aggregate(kvp, (seed, line) => 
{ 
    if(line.StartsWith("https")) 
     kvp.Last().Value.Add(line); 
    else 
     kvp.Add(
      new KeyValuePair<string,List<string>>(
       line, new List<string>() 
      ) 
     ); 
    } 
    return kvp; 
}); 
+0

사실 그것은 실제로 기능적 ... 아니요 (보이는) 루프 일뿐입니다 ... – digEmAll

+0

MSDN 문서에는 [F # 프로그램하는 방법]에 대한 정보가 있습니다 (http://msdn.microsoft.com/en- 우리/도서관/dd233154.aspx) ... –

+0

@ digEmAll : 그것은 비록 개체를 돌연변이. ('.Add()') – recursive

답변

6

따라서, 귀하의 의견은 다음과 같이 보인다. 다음은이 수행하는 하스켈 프로그램입니다 :

import Data.List (isPrefixOf) 

groupUrls :: [String] -> [(String, [String])] 
groupUrls [] = [] 
groupUrls (header:others) = (header, urls) : groupUrls remaining 
    where (urls, remaining) = span (isPrefixOf "https") others 

main = do 
    input <- readFile "\\\\ad1\\\\Users\\aanodide\\Desktop\\APIUserGuide.txt" 
    let slice = take (471 - 375 + 1) $ drop 374 $ lines input 
    let kvp = groupUrls slice 
    print kvp 

출력 :

[("Foo",["https://example1.com","https://example2.com"]),("Bar", ["https://example3.com"]),("Baz",[]),("Xyzzy",["https://example4.com"])] 

관심의 주요 기능은 여기 "https"로 시작하는 연속 선을 타고와 함께 그들을 돌아가려면 여기를 사용 span입니다 나머지 라인은 재귀 적으로 처리됩니다.

관련 문제