Commits

Maxim Moiseev  committed d38310e

added extension methods for IQueryable and IEnumerable

  • Participants
  • Parent commits 98e62ce

Comments (0)

Files changed (5)

File src/Wepr.Tests.CSharp/Wepr.Tests.CSharp.csproj

+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="3.5" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+  <PropertyGroup>
+    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
+    <ProductVersion>9.0.30729</ProductVersion>
+    <SchemaVersion>2.0</SchemaVersion>
+    <ProjectGuid>{B7FEED13-CE9D-4B11-AD1E-09D8946355D8}</ProjectGuid>
+    <OutputType>Library</OutputType>
+    <AppDesignerFolder>Properties</AppDesignerFolder>
+    <RootNamespace>Wepr.Tests.CSharp</RootNamespace>
+    <AssemblyName>Wepr.Tests.CSharp</AssemblyName>
+    <TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
+    <FileAlignment>512</FileAlignment>
+  </PropertyGroup>
+  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
+    <DebugSymbols>true</DebugSymbols>
+    <DebugType>full</DebugType>
+    <Optimize>false</Optimize>
+    <OutputPath>bin\Debug\</OutputPath>
+    <DefineConstants>DEBUG;TRACE</DefineConstants>
+    <ErrorReport>prompt</ErrorReport>
+    <WarningLevel>4</WarningLevel>
+  </PropertyGroup>
+  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
+    <DebugType>pdbonly</DebugType>
+    <Optimize>true</Optimize>
+    <OutputPath>bin\Release\</OutputPath>
+    <DefineConstants>TRACE</DefineConstants>
+    <ErrorReport>prompt</ErrorReport>
+    <WarningLevel>4</WarningLevel>
+  </PropertyGroup>
+  <ItemGroup>
+    <Reference Include="FSharp.Core, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
+      <Private>True</Private>
+    </Reference>
+    <Reference Include="nunit.framework, Version=2.2.7.0, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77">
+      <SpecificVersion>False</SpecificVersion>
+      <HintPath>..\..\lib\nunit.framework.dll</HintPath>
+    </Reference>
+    <Reference Include="System" />
+    <Reference Include="System.Core">
+      <RequiredTargetFramework>3.5</RequiredTargetFramework>
+      <Private>True</Private>
+    </Reference>
+    <Reference Include="System.Xml.Linq">
+      <RequiredTargetFramework>3.5</RequiredTargetFramework>
+    </Reference>
+    <Reference Include="System.Data.DataSetExtensions">
+      <RequiredTargetFramework>3.5</RequiredTargetFramework>
+    </Reference>
+    <Reference Include="System.Data" />
+    <Reference Include="System.Xml" />
+  </ItemGroup>
+  <ItemGroup>
+    <ProjectReference Include="..\Wepr\Wepr.fsproj">
+      <Project>{95321677-A32E-45DA-826C-769EC4F921DB}</Project>
+      <Name>Wepr</Name>
+    </ProjectReference>
+  </ItemGroup>
+  <ItemGroup>
+    <Compile Include="WeprTests.cs" />
+  </ItemGroup>
+  <ItemGroup>
+    <Folder Include="Properties\" />
+  </ItemGroup>
+  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
+  <!-- To modify your build process, add your task inside one of the targets below and uncomment it. 
+       Other similar extension points exist, see Microsoft.Common.targets.
+  <Target Name="BeforeBuild">
+  </Target>
+  <Target Name="AfterBuild">
+  </Target>
+  -->
+</Project>

File src/Wepr.Tests.CSharp/WeprTests.cs

+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Linq.Expressions;
+using System.Text;
+
+using NUnit.Framework;
+
+using Wepr;
+
+namespace Wepr.Tests.CSharp {
+
+    [TestFixture]
+    public class WeprTests {
+
+        [Test]
+        public void WeprExtensionTestShouldParseStringIntoExpression() {
+            string[] strings = new string[] { "1", "123", "12", "12ab", "a", "ab", "abc" };
+
+            //var res = from s in strings
+            //          where "Length >= 3"
+            //          select s;
+            var res = strings.Where("Length >= 3").ToArray();
+            Assert.AreEqual(3, res.Length);
+        }
+
+    }
+}

File src/Wepr.sln

 EndProject
 Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "Wepr.Tests", "Wepr.Tests\Wepr.Tests.fsproj", "{C5AC305F-3147-4F26-AF6C-E57AFC7BF804}"
 EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Wepr.Tests.CSharp", "Wepr.Tests.CSharp\Wepr.Tests.CSharp.csproj", "{B7FEED13-CE9D-4B11-AD1E-09D8946355D8}"
+EndProject
 Global
 	GlobalSection(SolutionConfigurationPlatforms) = preSolution
 		Debug|Any CPU = Debug|Any CPU
 		{C5AC305F-3147-4F26-AF6C-E57AFC7BF804}.Debug|Any CPU.Build.0 = Debug|Any CPU
 		{C5AC305F-3147-4F26-AF6C-E57AFC7BF804}.Release|Any CPU.ActiveCfg = Release|Any CPU
 		{C5AC305F-3147-4F26-AF6C-E57AFC7BF804}.Release|Any CPU.Build.0 = Release|Any CPU
+		{B7FEED13-CE9D-4B11-AD1E-09D8946355D8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+		{B7FEED13-CE9D-4B11-AD1E-09D8946355D8}.Debug|Any CPU.Build.0 = Debug|Any CPU
+		{B7FEED13-CE9D-4B11-AD1E-09D8946355D8}.Release|Any CPU.ActiveCfg = Release|Any CPU
+		{B7FEED13-CE9D-4B11-AD1E-09D8946355D8}.Release|Any CPU.Build.0 = Release|Any CPU
 	EndGlobalSection
 	GlobalSection(SolutionProperties) = preSolution
 		HideSolutionNode = FALSE

File src/Wepr/Wepr.fsproj

     <Compile Include="parser.fs" />
     <Compile Include="lexer.fs" />
     <Compile Include="expr.fs" />
+    <Compile Include="ext.fs" />
     <None Include="Sample.fsx" />
     <None Include="FoldTest.fsx" />
   </ItemGroup>

File src/Wepr/ext.fs

+namespace Wepr
+
+open System
+open System.Linq
+open System.Linq.Expressions
+open System.Collections.Generic
+
+
+module Extensions =
+
+    // F# extension methods
+    type System.Linq.IQueryable<'TSource> with
+    
+        member this.Where (s:String) =
+            let expr = Wepr.GetExpression<'TSource> s :?> Expression<Func<'TSource, bool>>
+            Queryable.Where(this, expr)
+            
+    type System.Collections.Generic.IEnumerable<'TSource> with
+    
+        member this.Where (s:String) =
+            let dlg = Wepr.Compile<'TSource> s
+            let predicate (arg: 'TSource) = dlg.DynamicInvoke arg :?> bool
+            Enumerable.Where(this, predicate)
+            
+[<System.Runtime.CompilerServices.Extension>]            
+module QueryableExtensions =
+    open Extensions
+    // C#-visible extension methods
+    [<System.Runtime.CompilerServices.Extension>]
+    let Where (src:IQueryable<'a>, where:String) = src.Where(where)
+    
+[<System.Runtime.CompilerServices.Extension>]            
+module EnumerableExtensions =    
+    open Extensions
+    [<System.Runtime.CompilerServices.Extension>]
+    let Where (src:IEnumerable<'a>, where:String) = src.Where(where)
+
+