함수 내에서 별도의 개인 함수를 사용해야하는시기는 언제입니까? 함수 내에서 별도의 개인 함수를 사용해야하는시기는 언제입니까?
나는 내가 쓴 함수가 상당히 긴 것을 관찰 :
let optionsFor piece (positions:Space list) =
let yDirection = match piece with
| Black _ -> -1
| Red _ -> 1
let sourceX , sourceY =
match piece with
| Black (checker , pos) -> pos
| Red (checker , pos) -> pos
let optionsForPiece =
(fun pos -> pos = ((sourceX - 1) , (sourceY + yDirection)) ||
pos = ((sourceX + 1) , (sourceY + yDirection)))
let availableSelection =
(fun space -> match space with
| Available pos -> Some pos
| Allocated _ -> None)
let availablePositions =
positions |> List.filter toAvailable
|> List.choose availableSelection
availablePositions |> List.filter optionsForPiece
따라서, 나는 여러 개의 작은 함수로 위의 기능을 리팩토링 고려했다.
그러나 함수 프로그래밍에 이것이 필요한지 확실하지 않습니다.
내부 기능에 대한 현재 권장 사항과 비공개 기능으로 추출하는 기능은 무엇이 있습니까?
부록 :
open NUnit.Framework
open FsUnit
(* Types *)
type Black = BlackKing | BlackSoldier
type Red = RedKing | RedSoldier
type Coordinate = int * int
type Piece =
| Black of Black * Coordinate
| Red of Red * Coordinate
type Space =
| Allocated of Piece
| Available of Coordinate
type Status =
| BlacksTurn | RedsTurn
| BlackWins | RedWins
(* Functions *)
let black coordinate = Allocated (Black (BlackSoldier , coordinate))
let red coordinate = Allocated (Red (RedSoldier , coordinate))
let startGame() =
[ red (0,0); red (2,0); red (4,0); red (6,0)
red (1,1); red (3,1); red (5,1); red (7,1)
red (0,2); red (2,2); red (4,2); red (6,2)
Available (1,3); Available (3,3); Available (5,3); Available (7,3)
Available (0,4); Available (2,4); Available (4,4); Available (6,4)
black (1,5); black (3,5); black (5,5); black (7,5)
black (0,6); black (2,6); black (4,6); black (6,6)
black (1,7); black (3,7); black (5,7); black (7,7) ] , BlacksTurn
let private toAvailable =
(fun space -> match space with
| Available pos -> true
| _ -> false)
let available (positions:Space list) = positions |> List.filter toAvailable
let optionsFor piece (positions:Space list) =
let yDirection = match piece with
| Black _ -> -1
| Red _ -> 1
let sourceX , sourceY =
match piece with
| Black (checker , pos) -> pos
| Red (checker , pos) -> pos
let optionsForPiece =
(fun pos -> pos = ((sourceX - 1) , (sourceY + yDirection)) ||
pos = ((sourceX + 1) , (sourceY + yDirection)))
let availableSelection =
(fun space -> match space with
| Available pos -> Some pos
| Allocated _ -> None)
let availablePositions =
positions |> List.filter toAvailable
|> List.choose availableSelection
availablePositions |> List.filter optionsForPiece
이것이 [코드 검토] (http://codereview.stackexchange.com/)에 더 적합한 지 궁금합니다. – s952163
내 코드는 컨텍스트로 사용됩니다. 나는이 질문에 코드가 필요 없다고 생각한다. 나는 각 옵션에 대해 찬반 양론을 찾고 있다고 생각한다. –
이것이 무엇인지 묻는 것이 확실하지 않지만 사용법 및 외부 (기능 및 모듈 및 기타 .NET 코드)에서 함수에 액세스하려는 경우이 작업을 수행해야합니다. 많은 작은 함수와 내부 함수를 정의하는 것은 잘못된 것이 아닙니다. – s952163