더 편한 메모 작성을 위해 miryang-dev.tistory로 이사했습니다.
SupabaseScalaGithubReact NativeprojectdiscoveryRemixRescriptPurescriptnpmCloudflareReactNext.jsGOHyper-VTiberoGitAlgorithms, 2020년

퓨어스크립트 북 챕터6 두 번째 읽어보는 메모장

July 20, 2022

퓨어스크립트 북 챕터6 - Type Classes

Show Me!

  • Show 타입클래스
    • 문자열로 변환
class Show a where
  show :: a -> String

boolean을 문자열로 변환

instance showBoolean :: Show Boolean where
  show true = "true"
  show false = "false"

Common Type Classes

  • Eq 타입클래스
    • 같은지 비교
    • 유형이 같고, 값이 같아야 한다.
class Eq a where
  eq :: a -> a -> Boolean
  • 모노이드
    • Again, strings and arrays are simple examples of monoids.
    • empty 값을 이용해 자기 자신을 만들 수 있다?
"abc" append "" = "abc"
[a,b,c] append [] = [a,b,c]

Superclasses

  • 두 클래스 사이에 명확한 is-a 관계
  • 서브 클래스의 모든 멤버는 수퍼클래스의 멤버이다.
  • 123 > number > unknown

a 가 Ord, Show 타입클래스에 속하기만 하면 showCompare를 사용할 수 있다.

showCompare :: forall a. Ord a => Show a => a -> a -> String
showCompare a1 a2 | a1 < a2 =
  show a1 <> " is less than " <> show a2
showCompare a1 a2 | a1 > a2 =
  show a1 <> " is greater than " <> show a2
showCompare a1 a2 =
  show a1 <> " is equal to " <> show a2