Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 4 additions & 8 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,28 +47,24 @@ reportUnusedVariable = false
reportUnnecessaryIsInstance = true
reportUnnecessaryComparison = true
reportUnnecessaryCast = true
reportPrivateUsage = false # Getters/setters reference private members from outside the class
reportPrivateUsage = true
reportImportCycles = true
reportDuplicateImport = true
reportConstantRedefinition = true
reportOverlappingOverload = true
reportInconsistentConstructor = true
reportImplicitStringConcatenation = true

pythonVersion = "3.12"
typeCheckingMode = "standard"

[tool.ruff]
# Keep in sync with .pre-commit-config.yaml
line-length = 120
lint.ignore = []
lint.select = ["E", "W", "F", "I", "T", "RUF", "TID", "UP"]
target-version = "py312"
include =["*.py"]

[tool.ruff.lint]
select = ["E", "W", "F", "I", "T", "RUF", "TID", "UP"]
ignore = [
"F841" # Ignore unused variable warnings
]

[tool.ruff.lint.flake8-bugbear]
# These Rust extension module types are immutable (frozen)
extend-immutable-calls = [
Expand Down
1 change: 1 addition & 0 deletions src/Fable.Cli/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

* [Python] Add `Array.skipWhile` support (by @dbrattli)
* [Python] Add `Array.takeWhile` support (by @dbrattli)
* [Python] Allow `IEnumerator_1` as base class to fix typing issues (by @dbrattli)
* [Python] Add Pythonic import path syntax for relative imports (`.module`, `..parent`, `...grandparent`) (by @dbrattli)
* [Python] Add `[<Py.DecorateTemplate>]` attribute for creating custom decorator attributes (by @dbrattli)
Expand Down
1 change: 1 addition & 0 deletions src/Fable.Compiler/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

* [Python] Add `Array.skipWhile` support (by @dbrattli)
* [Python] Add `Array.takeWhile` support (by @dbrattli)
* [Python] Allow `IEnumerator_1` as base class to fix typing issues (by @dbrattli)
* [Python] Add Pythonic import path syntax for relative imports (`.module`, `..parent`, `...grandparent`) (by @dbrattli)
* [Python] Add `[<Py.DecorateTemplate>]` attribute for creating custom decorator attributes (by @dbrattli)
Expand Down
2 changes: 2 additions & 0 deletions src/fable-library-py/fable_library/array_.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@
unzip = array.unzip
copy = array.copy
take = array.take
take_while = array.take_while
skip = array.skip
skip_while = array.skip_while
compare_to = array.compare_to
Expand Down Expand Up @@ -222,6 +223,7 @@
"sum_by",
"tail",
"take",
"take_while",
"transpose",
"truncate",
"try_find",
Expand Down
4 changes: 4 additions & 0 deletions src/fable-library-py/fable_library/core/array.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ class FSharpArray[T](MutableSequence[T]):
def sum_by[U](self, projection: Callable[[T], U], adder: IGenericAdder[U]) -> U: ...
def tail(self, cons: FSharpCons[T] | None = None) -> FSharpArray[T]: ...
def take(self, count: SupportsInt, cons: FSharpCons[T] | None = None) -> FSharpArray[T]: ...
def take_while(self, predicate: Callable[[T], bool], cons: FSharpCons[T] | None = None) -> FSharpArray[T]: ...
def transpose(self, cons: FSharpCons[FSharpArray[T]] | None = None) -> FSharpArray[FSharpArray[T]]: ...
def truncate(self, count: SupportsInt) -> FSharpArray[T]: ...
def try_find(self, predicate: Callable[[T], bool]) -> T | None: ...
Expand Down Expand Up @@ -353,6 +354,9 @@ def sum[T](array: FSharpArray[T], adder: IGenericAdder[T]) -> T: ...
def sum_by[T, U](projection: Callable[[T], U], array: FSharpArray[T], adder: IGenericAdder[U]) -> U: ...
def tail[T](array: FSharpArray[T], cons: FSharpCons[T] | None = None) -> FSharpArray[T]: ...
def take[T](count: SupportsInt, array: FSharpArray[T], cons: FSharpCons[T] | None = None) -> FSharpArray[T]: ...
def take_while[T](
predicate: Callable[[T], bool], array: FSharpArray[T], cons: FSharpCons[T] | None = None
) -> FSharpArray[T]: ...
def transpose[T](
array: FSharpArray[FSharpArray[T]] | Iterable[FSharpArray[T]], cons: FSharpCons[FSharpArray[T]] | None = None
) -> FSharpArray[FSharpArray[T]]: ...
Expand Down
33 changes: 33 additions & 0 deletions src/fable-library-py/src/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1113,6 +1113,27 @@ impl FSharpArray {
self.skip(py, count as isize, cons)
}

#[pyo3(signature = (predicate, cons=None))]
pub fn take_while(
&self,
py: Python<'_>,
predicate: &Bound<'_, PyAny>,
cons: Option<&Bound<'_, PyAny>>,
) -> PyResult<FSharpArray> {
let len = self.storage.len();
let mut count = 0;

while count < len {
let item = self.get_item_at_index(count as isize, py)?;
if !predicate.call1((item,))?.is_truthy()? {
break;
}
count += 1;
}

self.take(py, count as isize, cons)
}

pub fn chunk_by_size(&self, py: Python<'_>, chunk_size: usize) -> PyResult<Self> {
if chunk_size < 1 {
return Err(PyErr::new::<exceptions::PyValueError, _>(
Expand Down Expand Up @@ -3234,6 +3255,17 @@ pub fn skip_while(
array.skip_while(py, predicate, cons)
}

#[pyfunction]
#[pyo3(signature = (predicate, array, cons=None))]
pub fn take_while(
py: Python<'_>,
predicate: &Bound<'_, PyAny>,
array: &FSharpArray,
cons: Option<&Bound<'_, PyAny>>,
) -> PyResult<FSharpArray> {
array.take_while(py, predicate, cons)
}

#[pyfunction]
pub fn chunk_by_size(
py: Python<'_>,
Expand Down Expand Up @@ -4551,6 +4583,7 @@ pub fn register_array_module(parent_module: &Bound<'_, PyModule>) -> PyResult<()
m.add_function(wrap_pyfunction!(sum_by, &m)?)?;
m.add_function(wrap_pyfunction!(tail, &m)?)?;
m.add_function(wrap_pyfunction!(take, &m)?)?;
m.add_function(wrap_pyfunction!(take_while, &m)?)?;
m.add_function(wrap_pyfunction!(transpose, &m)?)?;
m.add_function(wrap_pyfunction!(try_find, &m)?)?;
m.add_function(wrap_pyfunction!(try_find_back, &m)?)?;
Expand Down
8 changes: 8 additions & 0 deletions tests/Python/TestArray.fs
Original file line number Diff line number Diff line change
Expand Up @@ -735,6 +735,14 @@ let ``test Array.skipWhile works`` () =
xs |> Array.skipWhile (fun x -> x < 6) |> equal [||]
[||] |> Array.skipWhile (fun x -> x < 3) |> equal [||]

[<Fact>]
let ``test Array.takeWhile works`` () =
let xs = [|1; 2; 3; 4; 5|]
xs |> Array.takeWhile (fun x -> x < 3) |> equal [|1; 2|]
xs |> Array.takeWhile (fun x -> x < 1) |> equal [||]
xs |> Array.takeWhile (fun x -> x < 6) |> equal [|1; 2; 3; 4; 5|]
[||] |> Array.takeWhile (fun x -> x < 3) |> equal [||]

// [<Fact>]
// let ``test Array.sortDescending works`` () =
// let xs = [|3; 4; 1; -3; 2; 10|]
Expand Down
Loading